Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String matching

How to match the string "Net        Amount" (between Net and Amount there can be any number of spaces, including zero) with net amount?

Spaces between these two words can be any whitespace and exact matching of two string should be there. But Net Amount (first string with spaces) can be part of any string like Rate Net Amount or Rate CommissionNet Amount.

The match should be case-insensitive.

like image 202
Harikrishna Avatar asked Dec 07 '22 03:12

Harikrishna


1 Answers

Use regular expressions. Have a look at the System.Text.RegularExpressions namespace, namely the Regex class:

var regex = new RegEx("net(\s+)amount", RegexOptions.IgnoreCase);
//                    ^^^^^^^^^^^^^^^
//                        pattern

The argument string is what is called the regular expression pattern. Regular expression patterns describe what strings will match against it. They are expressed with a specialized syntax. Google for regular expressions and you should find plenty of information about regexes.

Usage example:

bool doesInputMatch = regex.IsMatch("nET      AmoUNT");
//                                  ^^^^^^^^^^^^^^^^^
//                                     test input
like image 59
stakx - no longer contributing Avatar answered Dec 09 '22 16:12

stakx - no longer contributing