mplace
mplace

Reputation: 65

Match any string that starts with... (###)

I'm new to regex and I'm trying to create an expression which matches any string which begins with:

Open Parenthesis, 3 numerical digits, followed by a closing parenthesis:

Examples of what should fit:

  1. (111)test
  2. (212) hello
  3. (321)

Should not work:

What I've created so far:

^(\d{3}^)* I've tried this on some online regex test sites but the matching isn't working.

What did I miss?

Upvotes: 1

Views: 86

Answers (1)

npinti
npinti

Reputation: 52185

You can try out something like so: ^\(\d{3}\).*$.

The above should match any string which starts with a parenthesis (not that the parenthesis is a special character in regex language, and thus it needs to be escaped, hence the extra \ at the beginning), is followed by 3 digits and a closing parenthesis (this is also a special character.

The regex will try to match zero or more repetitions of any other character before expecting to find the end of the string.

Your regular expression, other than needing to escape the parenthesis looks fine, however, it will look for zero or more repetitions of the parenthesis pattern. This means that it can also match foobar.

Upvotes: 3

Related Questions