1

This Regex is written with the .Net Regex Class.

So I have this string:

<div style="text-align:center;font-size: 18px;"><span style="font-size:14px;">11th of April 2015</span> 18:10</div>

I have this Regex Code:

[0-9]{1,2}(st|nd|rd|th) of \w{3,9} \d{4}<\/span> \d{1,2}:\d{1,2}

This Code return this Match:

11th of April 2015</span> 18:10

Is there a possibility with Regex to exclude the

(st|nd|rd|th) of 

and

</span>

from the Match to make it look like this:

11 April 2015 18:10

I have tried with positive lookbehind, but I didn't get it to work.

3
  • use html parser instead? Commented Apr 12, 2015 at 20:13
  • Is it JavaScript you are using? Commented Apr 12, 2015 at 20:14
  • No I am using C# Regex Commented Apr 12, 2015 at 20:15

3 Answers 3

3

You can use grouping for sub-string that you want and none capturing for the groups that you don't want:

(\d+)(?:st|nd|rd|th) [a-zA-Z]+ ([a-zA-Z]+) (\d+)<\/span>\s?(\d+:\d+)<\/div>

Demo

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

1 Comment

This results in 11, April, 2015 being captured. In OP, it is stated that 11 April 2015 18:10 must be the final result.
0

You can match the parts of the string that you need, and then combine the capture groups, e.g.:

var regex34 = new Regex(@"([0-9]{1,2})(?:(?:st|nd|rd|th) of)( \w{3,9} \d{4})<\/span>( \d{1,2}:\d{1,2})");
var input34 = "<div style=\"text-align:center;font-size: 18px;\"><span style=\"font-size:14px;\">11th of April 2015</span> 18:10</div>";
var result = regex34.Match(input34);
var final = result.Groups[1].Value + result.Groups[2].Value + result.Groups[3].Value;

Output:

enter image description here

2 Comments

@Encore: I gave the correct answer with testing results in VS2012 first.
Thank you for your answer, but I had the C# code already and just needed the new Regex, that was first provided by Kasra. Sorry!
0

You can use a non-capture group: (?:...)

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.