2

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.

1
  • I'm not a pro or anything... but if it were me I would split at "ddl_" and then read until I hit "_iy" Commented Jun 28, 2012 at 19:33

3 Answers 3

3

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

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

else

(:_ddl_)(\w+?)(?:_i\d+)
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.