Skip to main content
added 128 characters in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97

Let's simplify a bit the regex:

grep -E '^P [0-9]+ -?24\b' file2.txt 

The main issue from your regex (apart non needed complexity and readability) is that you use extra erroneous spaces.

No need:

If you think you can have more space than just one in your input, you can use one of:

  • +
  • [[:space:]]+ (POSIX character class)
  • \s+ require -P aka PCRE switch of grep

The regular expression matches as follows:

Node Explanation
^ start of the line anchor
P 'P' + space
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
a space
-? '-' (optional (matching the most amount possible))
24 '24'
\b word boundary

Let's simplify a bit the regex:

grep -E '^P [0-9]+ -?24\b' file2.txt 

No need:

If you think you can have more space than just one in your input, you can use one of:

  • +
  • [[:space:]]+ (POSIX character class)
  • \s+ require -P aka PCRE switch of grep

The regular expression matches as follows:

Node Explanation
^ start of the line anchor
P 'P' + space
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
a space
-? '-' (optional (matching the most amount possible))
24 '24'
\b word boundary

Let's simplify a bit the regex:

grep -E '^P [0-9]+ -?24\b' file2.txt 

The main issue from your regex (apart non needed complexity and readability) is that you use extra erroneous spaces.

No need:

If you think you can have more space than just one in your input, you can use one of:

  • +
  • [[:space:]]+ (POSIX character class)
  • \s+ require -P aka PCRE switch of grep

The regular expression matches as follows:

Node Explanation
^ start of the line anchor
P 'P' + space
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
a space
-? '-' (optional (matching the most amount possible))
24 '24'
\b word boundary
deleted 11 characters in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97

Let's simplify a bit with a simplerthe regex:

grep -E '^P [0-9]+ -?24\b' file2.txt 

No need:

If you think you can have more space than just one in your input, you can use one of:

  • +
  • [[:space:]]+ (POSIX character class)
  • \s+ require -P aka PCRE switch of grep

The regular expression matches as follows:

Node Explanation
^ start of the line anchor
P 'P' + space
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
a space
-? '-' (optional (matching the most amount possible))
24 '24'
\b word boundary

Let's simplify a bit with a simpler regex:

grep -E '^P [0-9]+ -?24\b' file2.txt 

No need:

If you think you can have more space than just one in your input, you can use one of:

  • +
  • [[:space:]]+ (POSIX character class)
  • \s+ require -P aka PCRE switch of grep

The regular expression matches as follows:

Node Explanation
^ start of the line anchor
P 'P' + space
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
a space
-? '-' (optional (matching the most amount possible))
24 '24'
\b word boundary

Let's simplify a bit the regex:

grep -E '^P [0-9]+ -?24\b' file2.txt 

No need:

If you think you can have more space than just one in your input, you can use one of:

  • +
  • [[:space:]]+ (POSIX character class)
  • \s+ require -P aka PCRE switch of grep

The regular expression matches as follows:

Node Explanation
^ start of the line anchor
P 'P' + space
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
a space
-? '-' (optional (matching the most amount possible))
24 '24'
\b word boundary
deleted 14 characters in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97

If you are ok, let'sLet's simplify a bit with a uniquesimpler regex:

grep -E '^P [0-9]+ -?24\b' file2.txt 

No need:

If you think you can have more space than just one in your input, you can use one of:

  • +
  • [[:space:]]+ (POSIX character class)
  • \s+ require -P aka PCRE switch of grep

The regular expression matches as follows:

Node Explanation
^ start of the line anchor
P 'P' + space
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
a space
-? '-' (optional (matching the most amount possible))
24 '24'
\b word boundary

If you are ok, let's simplify a bit with a unique regex:

grep -E '^P [0-9]+ -?24\b' file2.txt 

No need:

If you think you can have more space than just one in your input, you can use one of:

  • +
  • [[:space:]]+ (POSIX character class)
  • \s+ require -P aka PCRE switch of grep

The regular expression matches as follows:

Node Explanation
^ start of the line anchor
P 'P' + space
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
a space
-? '-' (optional (matching the most amount possible))
24 '24'
\b word boundary

Let's simplify a bit with a simpler regex:

grep -E '^P [0-9]+ -?24\b' file2.txt 

No need:

If you think you can have more space than just one in your input, you can use one of:

  • +
  • [[:space:]]+ (POSIX character class)
  • \s+ require -P aka PCRE switch of grep

The regular expression matches as follows:

Node Explanation
^ start of the line anchor
P 'P' + space
[0-9]+ any character of: '0' to '9' (1 or more times (matching the most amount possible))
a space
-? '-' (optional (matching the most amount possible))
24 '24'
\b word boundary
added 9 characters in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97
Loading
added 82 characters in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97
Loading
deleted 4 characters in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97
Loading
added 46 characters in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97
Loading
added 46 characters in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97
Loading
edited body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97
Loading
added 28 characters in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97
Loading
added 38 characters in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97
Loading
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97
Loading