tags:

views:

119

answers:

3

Hi, I'm trying to check a string and then extract all the variables which starts with @. I can't find the appropriate regular expression to check the string. The string may start with @ or " and if it's started with " it should have a matching pair ".

Example 1:

"ip : "+@value1+"."+@value2+"."+@value3+"."+@value4

Example 2:

@nameParameter "@yahoo.com"

Thanks

+1  A: 

It would probably be easiest to first split the string on each quoted string, then check the unquoted parts for @'s. For example all quoted strings could be: /"[^"]*"/, calling Regex.Split on your string would return an array of strings of the non-quoted parts, which you could then use the expression /@\w+/ to find any @'s.

Adam Luter
I tried to split the string with /"[^"]*"/ but it didn't work !!!System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"/""[^""]*/"""); string[] consts = reg.Split(valueToCheck);
Asha
What do you mean by it didn't work? I'm sorry.
Adam Luter
that was a syntax error I should have changed something .but thank you about the split suggestion .
Asha
A: 

Try this:


string text = "@nameParameter \"@yahoo.com\"";
Regex variables = new Regex(@"(?<!"")@\w+", RegexOptions.Compiled);
foreach (Match match in variables.Matches(text))
{
    Console.WriteLine(match.Value);
}
Rubens Farias
A: 

To check the strings you have provided in your post:

(^("[^"\r\n]"\s+@[\w.]+\s*+?)+)|(((^@[\w.]+)|("@[\w.]+"))\s*)+

skwllsp