0

I have a very simple string I am trying to match but can't seem to get it working. I want to match some numbers after a string and also emit the trailing %.

encodeTime=10%

I have tried look ahead but it doesn't seem to work. It just matches the entire string.

(?=encodeTime=)[^\s]+

Is there a way I can just match the numbers only and also emit the % after?

3
  • You couldn't find a regex that just matches a series of numeric digits? Did you look? Commented Aug 28, 2014 at 17:30
  • What do you mean by emit, do you mean omit ? Commented Aug 28, 2014 at 17:34
  • How about encodeTime=(\d*) ? Commented Aug 28, 2014 at 17:37

2 Answers 2

1

You need a lookbehind for this purpose and this (?=encodeTime=) is not a valid positive lookbehind.

(?<=encodeTime=)\d+

DEMO

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

Comments

1

You don't need any kind of lookaround assertion for something as simple as this. Use a capturing group and use the Match.Groups Property to reference the group and print your match result.

String s = "encodeTime=10%";
Match m  = Regex.Match(s, @"encodeTime=(\d+)");
if (m.Success)
   Console.WriteLine(m.Groups[1].Value); //=> "10"

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.