0

I just wanted to ask for an example of a string that would match this regular expression for JS:

 /\/[a-z]{2,6}\/(\([0-9]+\)?$/

But this bit confuses me: /(\([0-9]+\)?$/

If I could get an example of a string using this regular expression and a brief explanation, that would be enough to clear it up for me.

Thanks!

EDITED: Sorry for the trouble, I missed a parentheses, however I just want to clear up, a string that would match would be such as: /ab/(12345) or /abcdef/(1) etc right?

6
  • A slash, followed by 2 to 6 letters, followed by a slash, optionally followed by some digits. What's not to get? Commented Jan 5, 2014 at 20:38
  • 3
    Thing is, that looks like what it's supposed to be, but it's broken if it is. That bracket shouldn't be escaped. Looks like the paren before it should, considering the one after it is. But then that whole part should be wrapped in more parens in order for the ? to work as it looks like it should. Commented Jan 5, 2014 at 20:40
  • try it out in regex101.com/#javascript Commented Jan 5, 2014 at 20:41
  • rick.measham.id.au/paste/explain.pl and regexper.com are nice resources to explain regular expressions. But indeed, as @cHao mentioned this one looks broken. Commented Jan 5, 2014 at 20:41
  • 1
    We need to have some rule against editing regex questions...lol...the edit invalidated basically all of the answers. But for the record, the regex is still broken. There's no closing paren to match the opening one. Commented Jan 5, 2014 at 20:58

3 Answers 3

2

Here is a visual representation of the @Paul Roub explanation:

EDITED with last pattern

\/[a-z]{2,6}\/\([0-9]+\)$

Regular expression visualization

Debuggex Demo

This example is OK : /abcdef/(12345)

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

3 Comments

There's stuff that's escaped in the OP's pattern that isn't in this rendition.
Great effort, but the asker changed the pattern again xD.
Very easy to understand! Thanks again everyone
2
\/

A forward slash (escaped), followed by

[a-z]{2,6}

between two and six lowercase English letters, followed by

\/

another slash

([0-9]+)?

the inside part - one or more digits. ? means "zero or one", so we're looking for a string of digits, or nothing. The parentheses would let this number be captured as a group for later processing

$

and the end of the string.

Things that would match:

/ab/0
/ab/
/acdefg/12345

things that would not match

/a/0
/abcdefgh/12345
/ab/0x

3 Comments

There's stuff that's escaped in the OP's pattern that isn't in this rendition. Some of it, depending on interpretation, could very well change what's supposed to be matched.
Ah. The OPs version has changed a few times, I was basing it (I think) on what was there at the time I answered.
Great answer. Cleared up my confusion, I did have an error like others pointed out in this, where I missed the parentheses, the actual regex is: /\/[a-z]{2,6}\/(([0-9]+)?$/ So would the matching strings be such as /ab/(12345) etc?
2

That regexp is malformed.

  • Opening bracket is escaped, but closing one not
  • Opening paren is not escaped but closing one is

I think the intended regexp was:

/\/[a-z]{2,6}\/([0-9]+)?$/

This would match:

/ab/1
/ab/
/abc/123

Wouldn't match:

/a/1
ab/1
/abcdefg/123

Cheers.

2 Comments

The ? does make sense if you wanted to match optional digits. (Course, you could also just say [0-9]*.)
Heh..you're gonna love this. The OP's regex changed again. :P

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.