Johnston
Johnston

Reputation: 20884

Regular expression for any letter or number followed by a parenthesis

Apparently I need some help with regular expressions. I am trying to find any letter or number followed by a parenthesis. l) 9) R).

I tried a couple of things. My idea was "starts with any letter or number followed by 1 parenthesis." So I tried this:

^[A-Za-z0-9]\({1}

Obviously it did not work.

Upvotes: 0

Views: 1395

Answers (2)

mrtimdog
mrtimdog

Reputation: 497

Strictly speaking, for what you're asking, your after:

[a-zA-Z0-9][\(\)]

Your example's looking for just open parenthesis, and just at the very start of a line, so that would be:

^[a-zA-Z0-9]\(

So you're correct. Maybe you're having issues with the quoting of '('s, but that would depend on how you're using/supplying the regex, and to what. Or, maybe you've got other characters before the number, eg. spaces and/or tabs, which would not be matched?

Upvotes: 1

M A
M A

Reputation: 72854

You're matching against an opening parenthesis whereas the example strings contain a closing one. You can match either of them using the below regex (there's no need for the {1} quantifier):

[A-Za-z0-9][()]

[()] matches either ( or ) -- there's no need to escape them when they are in square brackets.

Upvotes: 5

Related Questions