0

I have string: text and something and something like f213 @@ -1,9 +1,11 @@ etc etc and litle more etc etc

i woul dlike to get now @@ -1,9 +1,11 @@. Numbers are not fixed and can be changed...

My regex which not working:

(@@ -)([0-9]+)(,)([0-9]+)( +)([0-9]+)(,)([0-9]+)( @@)

Can someone see error in my regex?

4
  • ( +) should be ( \+) It's a really weird regexp, though. Commented Mar 27, 2012 at 14:09
  • They don't make the regex wrong, but you also don't need any of your round brackets, unless you want to capture e.g. the numbers themselves. @@ -[0-9]+,[0-9]+ \+[0-9]+,[0-9]+ @@ Commented Mar 27, 2012 at 14:13
  • How about an actual snipet of data and not saying etc, etc,etc. Provide all different combinations. -1 Commented Mar 27, 2012 at 14:18
  • 1
    @OmegaMan you mean you can't tell from the regex the OP provided what's being asked? Downvote's a bit harsh... Commented Mar 27, 2012 at 14:22

3 Answers 3

3

You need to escape your plus:

(@@ -)([0-9]+)(,)([0-9]+)( \+)([0-9]+)(,)([0-9]+)( @@)

By the way, this is the best tool ever: http://www.nregex.com/nregex/default.aspx when trying to see what a regex is doing.

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

Comments

1

Maybe an other solution :

Regex.IsMatch(yourString, @"@@\s(.+?)\s(.+?)\s@@", RegexOptions.Singleline);

With your exemple, the group 1 will be "-1,9" and the group 2 "+1,11".

Comments

0

Have you considered using String.Split()?

string input = "f213 @@ -1,9 +1,11 @@";
string[] fields = input.Split(new[] { "@@" }, StringSplitOptions.None);
string result = "@@" + fields[1] + "@@";

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.