3

I have a string:

\r\ndsadasdsad\das\rdasdsacxz\ndasdsa\r\nadsadas\e

I want to make a regexp that will match all characters with '\' in front of them, but not "\r\n", so it would be '\.' without '\r\n'

georg
  • 204,715
  • 48
  • 286
  • 369
karlkar
  • 227
  • 5
  • 12

4 Answers4

3
\\r(?!\\n)|(?<!\\r)\\n|\\[^rn]

Live demo

revo
  • 45,845
  • 14
  • 70
  • 113
0

This regex should match a single character that is preceded by a \, but is not part of the sequence \r\n:

(?:(?<!\\)|(?!r\\n))(?:(?<!\\r\\)|(?!n))(?<=\\).

You can find an explanation here.

The Guy with The Hat
  • 10,290
  • 8
  • 59
  • 73
-1

This will match all characters which are not "n" or "r" and which have a slash in front of them.

(?<=\\)[^rn]
mtanti
  • 764
  • 8
  • 23
  • 1
    So it will not match \r or \n separately - I want them to be matched if they not occur together. Only when there's '\r\n' I want them not to be matched. – karlkar Jan 07 '14 at 17:00
-1

Ok, this should do what you're asking.. :

As per your question this matches "ALL characters with '\' in front of them, but not '\r\n'"

Test String:

\r\ndsadasdsad\das\rdasdsacxz\ndasdsa\r\nadsadas\e

Regex:

(?:\\r\\n\w*)|(\w+)

Matches:

MATCH 1 'das'

MATCH 2 'rdasdsacxz'

MATCH 3 'ndasdsa'

MATCH 4 'e'

Here's an example: http://regex101.com/r/lE7gI7

Bryan Elliott
  • 3,925
  • 2
  • 20
  • 22