Reputation: 3169
I have this expression
accordionMain_i0_accordion_i0_ctl01_0_ddl_xxxxxxxx_iy_tblItem
where xxx can contain also '_' and its number vary, and y start from 0 by incrementing. I would like to extract the xxxxxx using regular expression. I tried split function but the xxxx can contain several '_' , so by removing the last 2 element of the array from split I have to check for ddl, then join all part of xxx etc. Do you know a better method by using REGEX? Any help would be greatly appreciated.
Upvotes: 2
Views: 1517
Reputation: 30273
You should specify the language you're using. But here's a stab at it:
/accordionMain_i0_accordion_i0_ctl01_0_ddl_([x_]+)_i\d+_tblItem/
Then, your x's will be stored in the first capture, $1
.
Here is a demo: http://rubular.com/r/MH4GbMStdf.
EDIT:
If you meant "anything" instead of literal "x", then:
/accordionMain_i0_accordion_i0_ctl01_0_ddl_(.+)_i\d+_tblItem/
Upvotes: 1
Reputation: 11182
If you RegEx engine supports lookahead
and lookbehind
, then use:
(?<=_ddl_)\w+?(?=_i\d+)
else
(:_ddl_)(\w+?)(?:_i\d+)
Upvotes: 3
Reputation: 43663
Regex accordionMain_i0_accordion_i0_ctl01_0_ddl_(.*?)_i\d+_tblItem
returns your xxxxxxxx if match found.
Upvotes: 2