1

Can you help me to understand / convert follwoing .net regex into java

@"(?<!\\)(?'M'[^|%])" ?
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
Rajender Saini
  • 594
  • 2
  • 9
  • 24

1 Answers1

1

This regex consists of two groups.

The first one (?<!\\) is a lookbehind assertion. It will match only if the previous letter is not a backslash. The second one (?'M'[^|%]) is a named capturing group (called M), that matches any character except "|" and "%".

I.e. the regex will match "a", and not match "\a" or "%".

Java does not support the named capture, but

(?<!\\)([^|%])

should work fine for you. You'd reference the first group by number, instead of by name then.

Note that you may have to escape backslashes leading to (?<!\\\\) for the first part.

Jens
  • 25,229
  • 9
  • 75
  • 117
  • i have converted it to "(?<!\\\\)(?[$|^])" as suggested by you. but i am getting error – Rajender Saini Mar 09 '11 at 15:20
  • i have converted it to "(?<!\\\\)(?[$|^])" as suggested by you. but i am getting error java.util.regex.PatternSyntaxException: Unknown inline modifier near index 9 (?<!\\)(?[$|^]) ^ at java.util.regex.Pattern.error(Unknown Source) – Rajender Saini Mar 09 '11 at 16:33