Charlie Brown
Charlie Brown

Reputation: 31

How come this RegEx isn't working quite right?

I have this RegEx here:

/^function(\d)$/

It matches function(5) but not function(55). How come?

Upvotes: 2

Views: 61

Answers (3)

Mike Schall
Mike Schall

Reputation: 5899

The other posters are correct about the +, but what language are you using for to parse the regular expression? Shouldn't you have to escape the ()? Otherwise it should capture the digit(s).

I would think you would need...

/^function\(\d+\)$/

Upvotes: 6

Frank V
Frank V

Reputation: 25419

/^function(\d+)$/

You need to add the + to make the \d (digits) greedy -- to match as much as possible. (Assuming that is what you are after as it would probably match

function(3242345235234235235234234234535325234235235234523) as well as function(55)

Repeats the previous item once or more. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only once.

referring to +

http://www.regular-expressions.info/reference.html

Upvotes: 5

bmargulies
bmargulies

Reputation: 100050

Because you only gave it one \d. If you want to match more than one digit, tell it so.

Upvotes: 0

Related Questions