0

I'm needing to extract a string of 3 numbers between a beginning character and a possibility of three different ending characters. Staring with _a and ending with either _b, _c or _d. I know how to do it with strfind but really want to know how to use regexp.

For example string could be:

ggggga_a123_cggggg

or/

gggggg_a345_bggggg

or/

gggggg_a456_dggggg

How do I just extract the three numbers without knowing which ending character it will be. Thanks

4
  • 1
    What regular expression(s) have you tried? Commented Sep 3, 2016 at 3:11
  • I just did regexp(s,'(?<=_a).*(?=b_|c_|d_)','match'); Should this work for all instances? Commented Sep 3, 2016 at 4:01
  • That's incorrect. You need this: regexp(s,'(?<=_a).*(?=_b|_c|_d)','match') Commented Sep 3, 2016 at 4:12
  • Thanks that worked Commented Sep 4, 2016 at 1:40

1 Answer 1

1

I'd go with a regex like this:

_a(\d{3})_[bcd]

What this will do is match the starting _a, match the 3 digits in a capture group (the ()) for extraction, then match the second underscore and one of b, c, or d.

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

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.