0

I ran this example in my bash terminal:

echo "ab" | egrep '\a\b' 

Output

ab 

then I ran this one:

echo "a" | egrep '\a\b' 

Output

a 

I was confused. Why did I get output? But then I input another one:

echo "a" | egrep '\ab' 

and didn't get output.

What is the difference between \a\b and \ab for Extended RegEx?

P.S

Regex didn't ignore the first letter:

echo "b" | egrep '\a\b' 

Output is empty Regex acted as I expected, but what is the difference from the first case?

P.P.S I found out that this example:

echo 'ab' | egrep '\a\b' 

also does not output anything. In the first case (echo "ab" | egrep '\a\b'), the output was ab. How can quotes affect regex?

4
  • In grep (GNU grep) 3.1, echo "ab" | egrep '\a\b' does not return any result. Commented Oct 29, 2020 at 20:16
  • \b is special, \a is just a more complicated way how to write a. Commented Oct 29, 2020 at 20:17
  • basically \b is like $ your examples. Commented Oct 29, 2020 at 20:20
  • Ok, I realized that escaping letters is not safe. But what about the other characters? Do they have other meanings than themselves when escaping? Or is it only letters that have similar properties? Commented Oct 29, 2020 at 20:25

1 Answer 1

1

The '\a\b' makes \a\b regex where \a is an unknown escape sequence and matches an a and \b is a word boundary.

echo "a" | egrep '\a\b' - correctly outputs a, there is an a not followed with a digit, letter or underscore.

echo "a" | egrep '\ab' - correctly does not output anything, a does not contain ab.

echo "b" | egrep '\a\b' - correctly does not output anything, b does not contain a.

As for echo "ab" | egrep '\a\b', it should not output any result, ab does not contain a at a word boundary, there is b after a.

Other letters that means something else when escaped (GNU grep, POSIX):

  • \B - a position other than a word boundary position
  • \s - whitespace
  • \S - non-whitespace
  • \w - letters, digits, and underscores
  • \W - characters different from letters, digits, and underscores.

Also, there is \< (start of word) and \> (end of word).

If you use -P, PCRE pattern, there are even more escape sequences available (see pcresyntax and pcrepattern).

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I realized that escaping letters is not safe. But what about the other characters? Do they have other meanings than themselves when escaping? Or is it only letters that have similar properties?
Or, escaping without consequences only applies to special characters such as: ^, $, etc. If so, is there some kind of list of all such symbols?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.