Encore
Encore

Reputation: 275

Exclude Substring from Match

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.

Upvotes: 1

Views: 106

Answers (3)

Kasravnd
Kasravnd

Reputation: 107287

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

Upvotes: 3

Jason Cust
Jason Cust

Reputation: 10899

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

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626728

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

Upvotes: 0

Related Questions