0

I have the following set of strings:

some_param[name] 
some_param_0[name]

I wish to capture some_param, 0, name from them. My regex knowledge is pretty weak. I tried the following, but it doesn't work for both cases.

/^(\D+)_?(\d{0,2})\[?(.*?)\]?$/.exec("some_param_0[name]") //works except for the trailing underscore on "some_param"

What would be the correct regex?

3 Answers 3

3
/^(\w+?)_?(\d{0,2})(?:\[([^\[\]]*)\])?$/

(\w+?) uses a non-greedy quantifier to capture the identifier part without any trailing _.

_? is greedy so will beat the +? in the previous part.

(\d{0,2}) will capture 0-2 digits. It is greedy, so even if there is no _ between the identifier and digits, this will capture digits.

(?:...)? makes the square bracketed section optional.

\[([^\[\]]*)\] captures the contents of a square bracketed section that does not itself contain square brackets.

'some_param_0[name]'.match(/^(\w+?)_(\d{0,2})(?:\[([^\[\]]*)\])?$/)

produces an array like:

["some_param_0[name]",  // The matched content in group 0.
 "some_param",          // The portion before the digits in group 1.
 "0",                   // The digits in group 2.
 "name"]                // The contents of the [...] in group 3.

Note that the non-greedy quantifier might interact strangely with the bounded repetition in \d{0,2}.

'x1234[y]'.match(/^(\w+?)_?(\d{0,2})(?:\[([^\[\]]*)\])?$/)

yields

["x1234[y]","x12","34","y"]
Sign up to request clarification or add additional context in comments.

6 Comments

I think he wants to remove the trailing underscore.. am I right? I tried with /^([a-zA-Z_]+)(?:_(\d{0,2}))?(?:\[([^\[\]]*)\])?$/ but it looks like it doesn't work (at least, in Python)
@redShadow, the RegExp in the OP leaves it out of capturing group 1, so I assumed the poster wanted it in.
Sorry, In the code comment I mentioned that I ideally wanted the trailing underscore to be ignored ("some_param" instead of "some_param_"). Should've made it clear in the question.
@fenderplayer, redShadow, Edited to make sure that _ is not captured in group 1.
(I love infinite discussions on how to finely tune regular expressions.. you always end up learning something)
|
1

Got it! (taking from Mike's answer):

/^(\D+)(?:_(\d+))?(?:\[([^\]]*)\])/

'some_param[name]' => ('some_param', None, 'name')
'some_param_0[name]' => ('some_param', '0', 'name')

(at least, in Python it works)

UPDATE: A little extra I wrote fiddling with it, by making the result cleaner by using named groups:

^(?P<param>\D+)(?:_(?P<id>\d+))?(?:\[(?P<key>[^\]]*)\])

UPDATE:

4 Comments

Cool! Will check out named groups.
@fenderplayer, JavaScript does not have named groups.
I am testing named groups in javascript too; it looks like the syntax is different from the Python one; keep tuned as I'll write here when I find out how to use them in JS too.. :)
Ok, it looks like there is no way to do that in JS.. :( stackoverflow.com/questions/5367369
0

Please ,check the follwing regexp "(\w+)_(\d)[(\w+)]" yo can test it @ http://rubular.com/

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.