0

I have a pattern (?<=.*?\*)([\-\+])(.\d{0,2})(?=(d)) which will return -45 in a string *-45d using http://regexstorm.net/tester when I use this pattern in my c# code it doesn't return anything.

string text= "*-4d";
string pattern = @"(<?<=.*?\*)([\-\+])(.\d{0,2})(?=(d))";
var result= Regex.Match(text, pattern);

any ideas why this is?

dotNetFiddle

4
  • 1
    Shouldn't @"(<?<... be @"(?<...? Commented Jan 5, 2015 at 22:48
  • 2
    Your pattern in C# code different for the first line one. First is correct. Commented Jan 5, 2015 at 22:53
  • may be you have already tried this, but are you looking for something like this: [-+]\d{0,2}d? -> returns (+/- with two digits and an optional 'd') your pattern doesn't return anything Commented Jan 5, 2015 at 22:55
  • Thanks Yorye your suggestion worked in the .NET code. Grant, I tried my pattern in regexstorm.net/tester and it worked Commented Jan 5, 2015 at 22:59

1 Answer 1

1

I'm not sure what you thought the regex was supposed to match but most likely the problem is here:

 (<?<=.*?\*)

That will match something like this: <?<=some_arbitary_text_followed_by_a_*

If you want to do a negative look-behind, the syntax should be ?<= but there is also the problem of the .*? which is incompatible with a look-behind. Something like this should work though:

string pattern = @"(?<=\*)([\-\+])(.\d{0,2})(?=(d))";
Sign up to request clarification or add additional context in comments.

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.