2

I have the following reg ex:

"^((?!([\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*@((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3})))).)*$"

But it's not matching new lines as well:

https://regex101.com/r/nT6wK0/1

Any ideas how to make it match when there is a new line?

Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • Suggest using http://regexper.com to see your regex mapped out. [Example](http://regexper.com/#%5E((%3F!(%5B%5Cw!%23%24%25%26'*%2B%5C-%2F%3D%3F%5C%5E_%60%7B%7C%7D~%5D%2B(%5C.%5B%5Cw!%23%24%25%26'*%2B%5C-%2F%3D%3F%5C%5E_%60%7B%7C%7D~%5D%2B)*%40(((%5B%5C-%5Cw%5D%2B%5C.)%2B%5Ba-zA-Z%5D%7B2%2C4%7D)%7C((%5B0-9%5D%7B1%2C3%7D%5C.)%7B3%7D%5B0-9%5D%7B1%2C3%7D)))).)*%24) – Underverse May 20 '15 at 23:54
  • 1
    What are you trying to achieve? I'm curious... – Willem Van Onsem May 20 '15 at 23:55
  • I want jquery validate to check if an email address is contained within a description field – Jimmyt1988 May 20 '15 at 23:57
  • @Jimmyt1988: but the provided example does not contain an email adress and matches. Perhaps you should ommit the `^` and `$`. These are anchors, by dropping these, you search for "a pattern in the entire string", regardless of its position. By using word boundaries (`\b`), you can even ensure that it starts/end either at the beginning or with a spacing char between the match and the "environment". – Willem Van Onsem May 21 '15 at 00:00
  • Ah, you mean negative lookahead, so all fragments that do **not** contain an email address. – Willem Van Onsem May 21 '15 at 00:02
  • 1
    An example of the text being matched would be useful – Underverse May 21 '15 at 00:05
  • You can do the same thing more efficiently with this: `^(?s)(?:(?![a-zA-Z0-9.!#$%&'*+/=?^_\`{|}~-]+@[a-zA-Z0-9]).)*$` –  May 21 '15 at 00:10
  • @sln: I get an error on the provided regex, regardless of the engine on regex101... – Willem Van Onsem May 21 '15 at 00:12
  • And, without anchors, you let valid email addresses be matched. –  May 21 '15 at 00:13
  • @CommuSoft - I'm running it on Perl, not some fly-by-night pseudo regex site. –  May 21 '15 at 00:14
  • Well the comments suggest it should be a js-flavored regex. Shouldn't the kleene star bind with the dot `.` and the dot replaced by `[\S\s]`. Otherwise you should/can only write one character after the address. – Willem Van Onsem May 21 '15 at 00:15
  • I don't see any JS tags, and the point I was making is the OP is not going to get what he thinks out of this because of the nature of email regexes. You can't just put a negative email address to match inline text, it won't work unless the email regex is the _exact_ defacto regex. –  May 21 '15 at 00:19
  • I guess there are the uninformed who hope though. –  May 21 '15 at 00:22
  • This is for my C# application, there are things called data annotations that come out into jquery validation on the view. The answer i have ticked now checks perfectly. – Jimmyt1988 May 21 '15 at 00:33

2 Answers2

2

The . at the and actually means

All but a line break character. (source)

By replacing it with [\S\s], it means

All spacing characters and all non-spacing characters; so all characters.

Then it seems to work. You could have used other variants like [\W\w], [\D\d],...

So the "correct" regex (please don't take my word for it, first test this) is:

^((?!([\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*@((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))))[\S\s])*$

regex101 demo.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
1

Assuming that you only want to match the first line, you can add the multiline option (/m) to include the newline.

If you want the second line to be included you'll need to read ahead an extra line. How you do that depends on the regex engine: N in sed; getline in awk; -n in perl; ...