2

I have the following line :

3EAM7A 1 3 EI AMANDINE MRV SHP 70 W 0 SH3-A1 1 SHP 70W OVOIDE AI E27 SON PIA PLUS

I'd like to get the string : EI AMANDINE MRV SHP 70 W. So I decided to select the strings between 1 (can also be 2, 3 or 99) and 0 (can also be 1, 2, 3, 4 or 5).
I tried :

(0|1|2|3|99)(.*)(0|1|2|3|4|5)

But I have this result :

EAM7A 1 3 EI AMANDINE MRV SHP 70 W 0 SH3-A1 1 SHP 70W OVOIDE AI E

that is not what I want to obtain.

Do you have an idea in regex to make that selection work ?
Thanks !

1
  • 1
    Thank you very much to both of you for the answers ! It helps me a lot and you learned me different tricks to do so. Thanks again ! Commented Aug 13, 2015 at 14:16

5 Answers 5

2

You were pretty close! Try this:

\b(?:0|1|2|3|99) ([^0|1|2|3|99].*?) (?:0|1|2|3|4|5)\b

Regex101

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

Comments

2

I think that you want to match "word" 4 to 9?

Your desired match will be in group 1

^(\S+\s){3}((\S+\s){6})

Enable the multiline option if you have a whole file of subject strings.

Comments

1

You can try with:

\s(?:[0-3]|99)\s([A-Z].*?)\b(?:[0-5])\b

DEMO

and get string by group $1. Or if your language support look around, try:

(?<=\s[0-3]\s|99)[A-Z].+?(?=\s[0-5]\s)

DEMO

to get match directly.

Comments

1

Another solution that is based on matching all initial space + digit sequences:

\b(?:(?:[0-3]|99)\b\s*)+(.*?)\s*\b(?:[0-5])\b

See demo

The result is in Group 1. With \b(?:(?:[0-3]|99)\b\s*)+ the rightmost number from the allowed leading set is picked.

6 Comments

Your regular expression incorrectly captures EI AMANDINE MRV SHP 70 W if the number before it is not in the set 0,1,2,3,99
@iismathwizard: I see, I added support for any space + number after the first number from the set.
why? Op clearly stated the number will only be in the set [0-3|99]
I fixed it. And not [0-3|99], but ([0-3]|99).
@Whoever downvoted without a commet: you will have -1. That is why when downvoting, you need to declare what you did not like.
|
0

You can use following regex :

(?:(?:[0-3]|99)\s)+(.*?)\s(?:[0-5])\s

See demo https://regex101.com/r/iX6oE1/6

Also note that for matching a range of number you can use a character class instead of multiple OR.

3 Comments

op doesn't want to capture all of that. EI AMANDINE MRV SHP 70 W
@iismathwizard Yep, fixed!
Actually, it's still wrong. it captures the 3 just before the string.

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.