Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to pick a part of a word

Tags:

c#

regex

I have a text like this:

my text has $1 per Lap to someone. 

Could anyone tell me how to pick the per part from it. I know how to pick the $ amount. It's like this:

new Regex(@"\$\d+(?:\.\d+)?").Match(s.Comment1).Groups[0].ToString()

Any help would be highly appreciated.

like image 665
Sampath Avatar asked May 06 '15 07:05

Sampath


1 Answers

In case you have multiple substrings you need inside a larger string, you can use capturing groups.

To obtain the per part, use the following regex and grab the Groups[2].Value:

var str = "my text has $1 per Lap to someone. ";
var per_str = new Regex(@"(\$\d+(?:\.\d+)?)\s*(\p{L}+)").Match(str).Groups[2].Value;

Output:

enter image description here

The regex to capture per is \p{L}+ where \p{L} captures all Unicode letters (e.g. ф, ё), not just Latin script.

To get the number part, use the same regex, but grab Groups[1].Value:

var num_str = new Regex(@"(\$\d+(?:\.\d+)?)\s*(\p{L}+)").Match(str).Groups[1].Value;

Output:

enter image description here

And another tip: compile your regex first if you plan to use it multiple times during your app execution:

var rx = new Regex(@"(\$\d+(?:\.\d+)?)\s*(\p{L}+)", RegexOptions.Compiled);
var per_str = rx.Match(str).Groups[2].Value;
var num_str = rx.Match(str).Groups[1].Value;

In case you need just a number after $, just put the opening round bracket after it in the regex: @"\$(\d+(?:\.\d+)?)\s*(\p{L}+)".

And to get all groups in 1 go, you can use

var groups = rx.Matches(str).Cast<Match>().Select(p => new { num = p.Groups[1].Value, per = p.Groups[2].Value }).ToList();

enter image description here

EDIT:

If you just want to match per after the number, you can use @"(\$\d+(?:\.\d+)?)\s*(per)" or (case-insensitive) @"(\$\d+(?:\.\d+)?)\s*((?i:per\b))"

like image 91
Wiktor Stribiżew Avatar answered Oct 16 '22 16:10

Wiktor Stribiżew