0

I'm learning about regular expression, and they are divided into basic and extended.

Basic Regular Expression(BRE) uses meta-character [ ] ^ $ . *.

It's works on grep command well. But, when we use ls, echo or something else command, it works well, too.(For example, ls -al [abc]*.txt)

But, when I learn section about pathname expansion, it uses BRE's metacharcter.

So, I think they are same. Am I right?

A.Cho
  • 469

1 Answers1

2

Pathname expansion doesn't hold any special meaning for ^, $, or ..

  1. * in a BRE matches zero or more repetitions of the preceding character or group (so a* , a, aa, aaa, …). * in pathname expansion matches any string, independent of the previous character (so a*a, ab, a !!! and any ?? string whatsoever, … - note: it doesn't match the empty string, which the a* BRE does). Similarly for ?.
  2. Negation in [] is via a leading ^ in BRE and a leading ! in pathname expansion.
  3. . matches any character in BRE, whereas . has no special significance in a pathname pattern. It does have significance in what is matched, but not in the pattern itself.

Beyond some superficial similarities, they are very different. Some things are possible in BREs and impossible in pathname matching (for example, try matching a filename composed only of as).

muru
  • 72,889
  • I confirm $, . have no special meaning above pathname expansion. But, still works ^ character. When I use ls -al [^ab]abc.txt, it shows me about kabc.txt – A.Cho Feb 20 '16 at 04:10
  • @A.Cho According to POSIX: "A bracket expression starting with an unquoted circumflex character produces unspecified results" - so it leaves some room for the shell implementer to interpret it differently, and your shell supports it for negation in []. It isn't necessarily true for all compliant shells. Bash does, dash doesn't. – muru Feb 20 '16 at 05:17
  • @A.Cho: The point the muru is trying to make is that pathname expansion patterns (a.k.a. wildcards or globs) are automatically anchored at the beginning and the end.  For example, if you grep for foo, you may find catfood, and if you grep for foo.*bar, you may find catfood at bargain prices.  To find only foo you need to grep for ^foo$; to find only foo123%bar, etc., you need to grep for ^foo.*bar$.  But a wildcard of foo*bar *doesn't* match a file called catfood_bargain, so you don't need to do ls -al ^foo*bar$. – Scott - Слава Україні Feb 20 '16 at 06:55