Frenchi In LA
Frenchi In LA

Reputation: 3169

Regular Expression Extract string

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

Answers (3)

Andrew Cheong
Andrew Cheong

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

Cylian
Cylian

Reputation: 11182

If you RegEx engine supports lookahead and lookbehind, then use:

(?<=_ddl_)\w+?(?=_i\d+)

else

(:_ddl_)(\w+?)(?:_i\d+)

Upvotes: 3

Ωmega
Ωmega

Reputation: 43663

Regex accordionMain_i0_accordion_i0_ctl01_0_ddl_(.*?)_i\d+_tblItem returns your xxxxxxxx if match found.

Upvotes: 2

Related Questions