How to build a regular expression to match a fixed string separated by spaces?

I could never create a regular expression myself, and now I have a simple application that he needs. How to create a simple regular expression that matches:

  • Fixed row
  • No spaces / spaces
  • "=" char
  • No spaces / spaces
  • '(' char

I am currently using the following code to match whole words, but as you can see, it is quite limited in its 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"); 
+4
source share
4 answers

"any" in regex means * quantifier ("Kleene star"), which exactly means "previous token, randomly often". "

Please note that for this you obviously can only avoid a fixed word, not the rest.

 Regex.Matches(data, @"\b" + Regex.Escape(columnID) + @" *= *\(\b"); 

Also note that now we needed to close the bracket at the end first.

And, as Hans correctly noted in the comments, his general use of \s instead of space ; \s means spaces , which includes the usual spaces, tabs, and newlines.

+3
source

Here is a regex matching your requirements. Fixed line prefix.

 Regex.Matches(data, Regex.Escape(columnID) + @"\s*=\s*\("); 
+2
source

In regex, β€œ*” matches β€œ0 or more” of the previous expression, and β€œ+” matches one or more. "[]" will match any of the characters in brackets. Alternatively, you can use "[^]" to match "not these characters."

For your example, the following regex pattern should work (replace "fixedString" for any fixed string): "fixedString\s*=\s*\("

To learn more about regex, if your field was an arbitrary string, you can use the following: "(\b[a-zA-Z]+\b)\s*=\s*\("

To break it:

"(\b[a-zA-Z0-9]+\b)" will correspond to the word boundary, at least one alphanumeric character, and then the word boundary (so basically the word consists of nothing but alphanumeric characters )

"\s*" will match "No spaces / spaces"

"=" will match the equal sign

"\s*" see above

"\(" will match the character '(' (this must be escaped because '(' means the beginning of a complex expression in the regular expression.)

I recommend using http://www.regextester.com/ if you want to get practice with creating regular expression patterns.

Update: I accidentally put \w in spaces in the original message. \w represents the characters of the word (alphanumeric characters plus "_"). It has been replaced with the correct escape character \s .

+1
source

You said "any spaces", but depending on what the regex expression expresses, you seem to be looking for an optional single whitespace. If so, use a question mark.

 Regex.Matches(data, Regex.Escape(columnID) + @"\s?=\s?\("); 

The question mark in the regular expression means that the previous character (or group if you use parentheses) is optional.

If you are looking for one empty space that may or may not exist, do not use + or *, because * will match 0 or more spaces until the next character is satisfied, and the + symbol will match one or more white spaces until the next character will be satisfied.

+1
source

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


All Articles