0

I need to write a regular expression fails on the single occurrence of a carriage return. I have a regular expression that captures letters, numbers and special characters, I need to write an expression that basically fails if a carriage return is present in the same way an expression that's looking for numbers fails when you provide a letter.

This is my current expression:

^([\w ¬_@.&#`!\"£$%^*()+={[}:;'~<,>?/|]|[^\p{L}]){0,100}$ 

I need a way of saying if a carriage return is present, then nothing should match. I've done a bit of research and have found this is what I need to incorporate the following to my expression above: [^\r\n]+ to exclude carriage return but I'm not quite sure it's going to result in 0 matches if a carriage return is present.

hhamza
  • 17
  • Insert `(?!.*\r)` after `^`. – Wiktor Stribiżew Dec 17 '17 at 21:54
  • Hi @Wiktor, I tested it on the following site https://www.regextester.com/21 it still matches carriage returns. What I want is, 0 matches if there are any carriage returns. This is the expression I entered ^(?!.*\r)([\w ¬_@.`!\"£$%^*()+={[}:;'~<,>?/|]|[]|[^\p{L}]){0,100}$ – hhamza Dec 17 '17 at 21:57
  • [Look here](http://regexstorm.net/tester?p=%5e%28%3f!.*%5cr%29%28%5b%5cw+%c2%ac_%40.%26%23%60!%5c%22%c2%a3%24%25%5e*%28%29%2b%3d%7b%5b%7d%3a%3b%27%7e%3c%2c%3e%3f%2f%7c%5d%7c%5b%5e%5cp%7bL%7d%5d%29%7b0%2c100%7d%24&i=t1est%0d%0a23%0d%0afoo%0d%0abar%0d%0a304958%0d%0abar&o=m) where line breaks are CRLF, and only the last line is matched as expected since there is no CR on it. – Wiktor Stribiżew Dec 17 '17 at 22:01
  • @WiktorStribiżew Hi, right I understand, but if I have something like this: test1 CR test2 CR - the single instance of a carriage return should make everything void - that's what I want. Also, what does CRLF mean? – hhamza Dec 17 '17 at 22:06
  • You don't need a check for a CR if you use anchors, just put it where the sun don't shine `^([\w ¬_@.\`!\"£$%^*()+={[}:;'~<,>?/|]|[^\p{L}\r]){0,100}$` –  Dec 17 '17 at 22:09
  • the sun don't shine `[^\p{L}` here `\r]` –  Dec 17 '17 at 22:18
  • Inside the negated class `[^\p{L}]` is the obvious place to add what you disallow. If you put it there `[^\p{L}\r\n]` you have to add `\Z` as the end of string anchor. `\A([\w ¬_@.\`!\"£$%^*()+={[}:;'~<,>?/|]|[^\p{L}\r\n]){0,100}\z` will match faster. On the other hand, `^(?!.*\r)([\w ¬_@.\`!\"£$%^*()+={[}:;'~<,>?/|]|[]|[^\p{L}]){0,100}$` will fail quicker. –  Dec 18 '17 at 19:22

0 Answers0