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.
Asked
Active
Viewed 82 times
0

Frenchi In LA
- 3,139
- 3
- 25
- 41
-
Try this: `[^(]*` or `.*?(?=\()` or `\W(.*?)\(` – Downgoat Jun 06 '15 at 03:56
-
possible duplicate of [Regex: matching up to the first occurrence of a character](http://stackoverflow.com/questions/2013124/regex-matching-up-to-the-first-occurrence-of-a-character) – Downgoat Jun 06 '15 at 03:58
-
Does the string always start with XXXX? – Drakes Jun 06 '15 at 03:59
-
Can you add some sample data? – Federico Piazza Jun 06 '15 at 04:08
2 Answers
2
You can use the pattern
[^(]+
or to be more strict on 4 digits and _I
you can use
\d{4}_I

Drakes
- 23,254
- 3
- 51
- 94
1
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.

Downgoat
- 13,771
- 5
- 46
- 69