4

I've never been able to construct a regular expression on my own, and now I have a simple application that needs one. How do I construct a simple regex that matches:

  1. A fixed string
  2. No whitespace / any whitespace
  3. The '=' char
  4. No whitespace / any whitespace
  5. The '(' char

Currently I'm using the following code to match whole words, but as you can see its 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");
2
  • What is your ulimate goal? Can you provide example text? Commented Jun 7, 2012 at 19:19
  • I was asking myself, do you have other characters/strings after the open parenthesys? And what's is the content of columnID? Commented Jun 7, 2012 at 19:19

4 Answers 4

3

“any” in regex means the * quantifier (“Kleene star”), which exactly means “the previous token, arbitrarily often”.

Note that for this to work you obviously can escape only the fixed word, not the rest.

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

Also note that we now had to escape the opening parenthesis at the end by hand.

And, as Hans noted correctly in the comments, it’s common to use \s instead of the space ; \s stands for “whitespace”, which includes conventional spaces, tabs and newline characters.

Sign up to request clarification or add additional context in comments.

1 Comment

if required, replace @" *= * part of the regex string with @"\s*=\s* to match any whitespace, including tabs, carriage returns, and newlines.
2

Here is a regular expression that satisfies your requirements. Prefix with your fixed string.

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

Comments

1

In regex, "*" matches "0 or more" of the previous expression, while "+" matches one or more. "[]" will match on any of the characters within the brackets. In addition, you can use "[^]" to match "not these characters".

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

For the sake of learning more about regex, if your field was an arbitrary string, you could use the following: "(\b[a-zA-Z]+\b)\s*=\s*\("

To break it down:

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

"\s*" will match "No whitespace / any whitespace"

"=" will match the equals sign

"\s*" see above

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

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

Update: I accidentally put \w in for whitespace in the original post. \w represents word characters (Alphanumeric characters plus "_"). It has been replaced with the correct regex escape character \s.

2 Comments

I think OP wanted to search for fixed words rather than arbitrary words.
Thanks for pointing that out. I updated the post to include a pattern for fixed words, but I left in the other information for reference in creating other regex patterns.
1

You said "any whitespace", but based on what your regex expression says you seem to be looking for an optional, single whitespace. If this is the case then use the question mark.

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

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

If you're looking for a single whitespace that may or may not exist, then don't use + or *, because the * will match 0 or more white spaces until the next character is satisfied and the + will match one or more white spaces until the next character is satisfied.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.