0

What I want to capture is some variable/call expressions without or or and keywords. As the title says, what is the difference between (?!or|and)\w+ and [(?!or|and)\w]+ ? I'm sure those are different, because for the following pattern:

...
if (var_select is False) or (var_select in SOME_GLOBAL_ARRAY):
...

pcregrep -r 'if (?!or|and)\w+ is False' fails to capture this pattern, but pcregrep -r 'if [(?!or|and)\w]+ is False' does. What does [...] mean in the context of having (?!...) and \w together?

sangwoo-joh
  • 117
  • 6
  • `[` starts a character set, so `[(?!or|and)\w]+` is probably nonsensical - it will match any of the characters inside. – CertainPerformance Nov 17 '20 at 02:36
  • `[ ]` is a custom *character class*. `\w` is a [predefined](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#predef) character class, same as `[a-zA-Z_0-9]`. In a character class, the characters `(`, `?`, `!`, `|`, and `)` have no special meaning, and specifying the same character multiple times is just redundant. Which means that `[(?!or|and)\w]` matches any of the following characters: `a-z`, `A-Z`, `0-9`, `_`, `(`, `)`, `?`, `!`, and `|`. --- In contrast, `(?!or|and)\w+` matches `a-z`, `A-Z`, `0-9`, and `_`, as long as it doesn't *start with* `or` or `and`. – Andreas Nov 17 '20 at 02:42
  • @Andreas Wow, thanks for your clarification. Indeed, when I tried the bracket pattern without `(` and `)`, it cannot capture the code. – sangwoo-joh Nov 17 '20 at 03:25
  • @Andreas Then is it allowed to write `[\w()]+` to mean `[a-zA-Z_()]+`? – sangwoo-joh Nov 17 '20 at 03:32
  • 1
    Yes, if you look in the **documentation** at the example [character classes](https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#classes), you will see that `[a-d[m-p]]` is a *union*, and is the same as `[a-dm-p]`. Since `\w` is the same as `[a-zA-Z_]`, that means that `[\w()]` is the same as `[[a-zA-Z_]()]` and therefore the same as `[a-zA-Z_()]`. – Andreas Nov 17 '20 at 04:17
  • 1
    See https://www.regular-expressions.info/ to learn more about how regular expressions work, across languages. – Andreas Nov 17 '20 at 04:19
  • 1
    The [Stack Overflow `regex` tag info page](/tags/regex/info) also has information about several of your questions here, and pointers to more learning materials. – tripleee Nov 17 '20 at 06:29

0 Answers0