0

I have a large string as shown below:

99/34 12/34 This text is 22.67 22/23 33/34 Second text is like is 22.67 55/66 45/54 Third text is like is 32.27

and so on. I am trying to form a regex expression to extract all the substrings that start with "two digits, slash, two digits, one whitespace, two digits, slash, two digits, any character any number of repetitions,one . literal and two digits" from the large string.

The regex I tried is \d{2}/\d{2}\s{1}.*\.\d{2}. But, this returns the a single string "99/34 12/34 This text is 22.67 22/23 33/34 Second text is like is 22.67 55/66 45/54 Third text is like is 32.27". I would like to get this extracted as

99/34 12/34 This text is 22.67

22/23 33/34 Second text is like is 22.67

55/66 45/54 Third text is like is 32.27

How would I do this? I am using C# (.NET 4.5)

1
  • read about lazy (or non-greedy, or reluctant) and greedy quantifiers. Commented Jan 31, 2016 at 0:55

1 Answer 1

2

The problem lies in the greedy .* it will try to match as many characters as possible while still giving a match.

You can simply modify your regex thus

 \d{2}/\d{2}\s.*?\d{2}\.\d{2}

The ? after the * makes it not greedy and only consume (eat) as few characters as possible in order to find a match.

Note that I also changed \s{1} to \s as it was a single character to start with an qualifying it as exactly one does nothing but obfuscate the pattern.

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.