edid
edid

Reputation: 359

RegEx : what does @ means before (?

I discovered a strange behavior in my app with regular expression and @ character followed with (.
Here are two examples of my .Net JScript adapted language :

//Using .Net Regex class
echo(Regex.IsMatch(".DOMAIN.S", "^\\.DOMAIN\\.S(\\.\\w*|\\b)"));//True
echo(Regex.IsMatch(".DOMAIN.@", "^\\.DOMAIN\\.@(\\.\\w*|\\b)"));//False !!

//Using JScript syntax
var re = /^\.DOMAIN\.S(\.\w*|\b)/g;
echo(re.exec(".DOMAIN.S") != null); //Ok
re = /^\.DOMAIN\.@(\.\w*|\b)/g;
echo(re.exec(".DOMAIN.@") != null); //Null !!

Removing (.\w*|\b) makes the test ok. So I interpret that @ followed with a ( has a special meaning in regular expression.
1. What's the meaning of it ?
2. What should be the good expression ?

Thanks very much.
ED

Upvotes: 1

Views: 1226

Answers (2)

NPE
NPE

Reputation: 500367

It's not the @ that's causing you problems, it's the \b.

\b matches a word boundary: there is one at the end of ".DOMAIN.S" while there isn't one at the end of ".DOMAIN.@". This explains why you get a match in the first case but not the second.

Upvotes: 1

jb.
jb.

Reputation: 10341

Regex.IsMatch(".DOMAIN.@", "^\\.DOMAIN\\.@(\\.\\w*|\\b)") is returning false because ^\\.DOMAIN\\.@ is matching the entire string, and then you are looking for a period and word characters, which there are none, or a word boundary. According to regular-expression.info:

There are three different positions that qualify as word boundaries:

  • Before the first character in the string, if the first character is a
    word character.
  • After the last character in the string, if the last character is a
    word character.
  • Between two characters in the string, where one is a word character and the other is not a word character.

The at symbol is not a word character, so the second bullet is false.

So, to actually answer your question, the at symbol is not special in Regex.

Upvotes: 3

Related Questions