c# - How to construct a regex to match a fixed string separated by whitespace? -
i've never been able construct regular expression on own, , have simple application needs one. how construct simple regex matches:
- a fixed string
- no whitespace / whitespace
- the '=' char
- no whitespace / whitespace
- the '(' char
currently i'm using following code match whole words, can see quite limited in functionality.
regex.matches(data, @"\b" + regex.escape(columnid + "=(") + @"\b"); regex.matches(data, @"\b" + regex.escape(columnid + "= (") + @"\b"); regex.matches(data, @"\b" + regex.escape(columnid + " =(") + @"\b"); regex.matches(data, @"\b" + regex.escape(columnid + " = (") + @"\b");
“any” in regex means *
quantifier (“kleene star”), means “the previous token, arbitrarily often”.
note work can escape only fixed word, not rest.
regex.matches(data, @"\b" + regex.escape(columnid) + @" *= *\(\b");
also note had escape opening parenthesis @ end hand.
and, hans noted correctly in comments, it’s common use \s
instead of space ;
\s
stands “whitespace”, includes conventional spaces, tabs , newline characters.
Comments
Post a Comment