2

I saw a re like this:

regexp "hello\[^\\n]*06" $str

what is the \[^\\n]* trying to match?

user2131316
  • 3,111
  • 12
  • 39
  • 53
  • 2
    Are you sure about the "\" before the "[" ? It's strange. It looks like an error. – Denys Séguret Jun 10 '13 at 08:36
  • It must be a typo or a troll attempt, It should be `[^\\n]*` which means match anything except `\n` (line feed) zero or more times. – HamZa Jun 10 '13 at 08:38
  • I am sure there is a "\" before the "[" – user2131316 Jun 10 '13 at 08:39
  • @user2131316 Well do you need it ? What would you like to match ? Developers are humans, they can make mistakes. Anyways it doesn't make sense. – HamZa Jun 10 '13 at 08:41
  • hello HamZa, I read the code above, I do not understand how it is going to match – user2131316 Jun 10 '13 at 08:45
  • It matches anything BUT '\n' (not a line feed, an explicit '\' + 'n'). If you can give some context someone might be able to discover the motivation behind it. Edit: the '\' before '[' is very peculiar as was stated. This seems like an error. – Reut Sharabani Jun 10 '13 at 08:45
  • 5
    It's tcl. Square brackets in tcl perform command substitution just like backticks do in bash. So yes, the `\[` is necessary to avoid the language complaining about `command '^' not found` – slebetman Jun 10 '13 at 08:46
  • @slebetman If that is true, then why is the closing bracket not escaped `\]` ? – HamZa Jun 10 '13 at 08:48
  • 3
    @HamZa: It's not necessary since a closing bracket does not have any significant meaning to the language without the opening bracket. – slebetman Jun 10 '13 at 08:58

1 Answers1

5

In tcl, strings grouped by double quotes ("") undergo substitution when evaluated. As you know, tcl understands the following substitutions:

  • variable substututions: Any word that starts with the $ sign is substituted for its value.
  • command substutution: Any group of words between [ and ] is treated as a statement and the first word is called as a command (function).
  • escape substitution: Any backslash (\) is treated as an escape sequence and substituted accordingly.

Therefore, the following code:

regexp "hello\[^\\n]*06" $str

is equivalent to:

regexp {hello[^\n]*06} $str

So it's trying to match the string that contains the word "hello" and "06" without any newlines between them.

slebetman
  • 109,858
  • 19
  • 140
  • 171