3

Okay a bit of a weird one. I know you can do things a bit different and get what you want, but I am just curious whether the functionality exists somewhere or somehow in a single regex line.

Here is a sample expression:

(?s)^\\sqrt[^A-Za-z].*?(\{\\rho\})
              ^          ^
              1          2

Character 1 [^A-Za-z] is checking for a delimiter.

Character 2 \{ might be that delimiter. It also might be a space, or a ton of other random characters.

However, even if the delimiter is a space, \{ must exist, which means [ {] is not ideal.

Is it possible to just confirm that the spot filled by character 1 is not a letter, however not have it count as a character? The logic sort of being like;

if ("(?s)^\\sqrt[^A-Za-z]" matches) {
    Proceed to evaluate as "(?s)^\\sqrt.*?(\{\\rho\})"
}
Anon
  • 2,267
  • 3
  • 34
  • 51

1 Answers1

2

The logic you described fits the negative lookahead behavior: it makes sure the text after the current position does not match its pattern.

Use

(?s)^\\sqrt(?![A-Za-z]).*?(\{\\rho\})

enter image description here

Here, ^ matches the start of string, then \\sqrt matches \sqrt and after it, the regex engine asserts that there is no ASCII letter right after it with (?![A-Za-z]) negative lookahead. Then, .*?(\{\\rho\}) goes on to match the rest.

See the regex demo.

Also, for more details, see another SO thread describing negative lookahead behavior.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Works. Now I just need to wait 7 minutes to accept. Oh gosh, that makes things so damn easier. – Anon Oct 13 '16 at 19:14