0

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

4
  • 1
    this is kind of unclear - what are you trying to extract? for each of your examples, you should list what you want in your results array. Commented Oct 6, 2009 at 13:20
  • I think he wants all the @'s except when inside a quoted string. Commented Oct 6, 2009 at 13:23
  • Whenever you start looking for "matching pairs", you might want to starting thinking about something other than a regex. Not that it will be impossible to make the expression work; just that it will likely end up as more trouble than it's worth. Commented Oct 6, 2009 at 13:28
  • yes Adam is right those after @'s are my parameters and those in double quote (") pairs are the fixed values . Commented Oct 6, 2009 at 13:54

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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);
that was a syntax error I should have changed something .but thank you about the split suggestion .
0

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);
}

Comments

0

To check the strings you have provided in your post:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.