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.
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:
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:
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();
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))"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With