Frenchi In LA
Frenchi In LA

Reputation: 3169

Extract a word with REGEX

I have some expression Like XXXX_I(YYYY) with XXXX fix I integer (1,2,3, etc) and YYYY string could be anything, any length. Is it possible d'extract just XXXX_I. Any help would be greatly appreciated.

Upvotes: 0

Views: 87

Answers (2)

Downgoat
Downgoat

Reputation: 14371

You have a few options considering this is a very vague topic. A few options

[^(]*

Gets text except (until) (


.*?(?=\()

Gets text up to (


\W(.*?)\(

Gets word before (


\d{4}_I

Gets four digits I then any single character


\d{4}_I(?=\()

Gets four digits I any character then (


Which one would I use?

(?<=\W|^)\d{4}_I*?(?=\()

Is a very versatile solution which is still strict on enforcing the rules.

Or if you are using a more limited Regex flavor such as JavaScript:

(?:\W|^)\d{4}_I*?(?=\()

In which case you might have an extra space at the beginning but it would still work well.

Upvotes: 1

Drakes
Drakes

Reputation: 23670

You can use the pattern

[^(]+

or to be more strict on 4 digits and _I you can use

\d{4}_I

Upvotes: 2

Related Questions