Regular expression to select part of a word

I have a text like this:

my text has $1 per Lap to someone. 

Can someone tell me how to choose the per part from it. I know how to choose the amount of $ . This is something like this:

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

Any help would be greatly appreciated.

+6
source share
4 answers

As you said, per is a string type, the following regular expression can do the job for you:

 \$\d+\s([a-zA-Z]+) 

But if per contains numbers, you can use \w that match the word characters:

 \$\d+\s(\w+) 

Demo

Note that in this case, per is in the first capture group, and you need to extract the first group.

You can also use a positive appearance if you do not want to use grouping :

 (?<=\$\d+\s)[a-zA-Z]+ 

If per is a special word that you can check with the following regular expression:

 (?<=\$\d+\s)per 

Sort of:

 var per_str = new Regex(@'(?<=\$\d+\s)per').Match(str).Groups[0].Value; if (per_str != ''){ #dostuff } 
+1
source

If you have several substrings that you need inside a larger string, you can use capture groups.

To get the per part, use the following regex and take 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 re-expression for capturing per is \p{L}+ , where \p{L} captures all Unicode letters (for example, , ), and not just the Latin script.

To get the part number, use the same regular expression, but take Groups[1].Value :

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

Output:

enter image description here

And one more tip: compile your regular expression if you plan to use it several times during the execution of your application:

 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; 

If you just want the number after $ , just put the opening parenthesis after it in the regular expression: @"\$(\d+(?:\.\d+)?)\s*(\p{L}+)" .

And to get all the groups in 1, 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))"

+2
source
 (?<=\$\d+(?:\.\d+)?\s+)\S+ 

That should do it for you.

0
source

As @Sayse said, you don't need Regex here. I made two decisions without.

Check out the Demo or read the code:

 public static void Main() { var s = "my text has $1 per Lap to someone."; Console.WriteLine(Test(s)); Console.WriteLine(Test2(s)); } static object Test(string s) { var tab = s.Remove(s.IndexOf(" Lap")) // remove everything after " Lap" .Substring(s.IndexOf(" $") + 2) // remove everything before " $" .Split(' '); return new { Amount = tab[0], Per = tab[1] }; } static object Test2(string s) { var tab = s.Split(' '); var amount = tab.Single(t => t.StartsWith("$")).Substring(1); var per = tab[Array.FindIndex(tab, t => t.StartsWith("$")) + 1]; return new { Amount = amount, Per = per }; } 

Output

 { Amount = 1, Per = per } { Amount = 1, Per = per } 
0
source

Source: https://habr.com/ru/post/986635/


All Articles