0

I am using the following regular expression:

(?=([0-9]+\s+ok\s+[0-9]+\s+nok)) (?=(ltelidt|eiflmrj5))

My expression should find the following pattern:

  1. (Condition1): Number followed by ok, and then number followed by nok. eg: 13 ok 1 nok
  2. (Condition2): It should find either ltelidt or eiflmrj5
  3. The final expression should be something like this: condition1 and condition2.

However, my regular expression failed to find any pattern in the following sentence:

605sd25                  schklog_1FS     13 ok  1 nok        106m  24_R23F   eiflmrj5  d1us41  fd1d     tm1500  o1t2enb1010                  2m    2016-02-18 23:30:34  2016-02-19 07:14:01
John Rambo
  • 906
  • 1
  • 17
  • 37
  • It seems you are dealing with records where fields are separated by tabs (or several spaces). In this case do not use a regex, split the record line by spaces or tabs and check the fields you need with the value you want. The result will be faster. – Casimir et Hippolyte Feb 23 '16 at 22:44

3 Answers3

2

You're using lookaheads incorrectly. Instead, you can get rid of them, and just capture the text between your two groups:

([0-9]+\s+ok\s+[0-9]+\s+nok).*?(ltelidt|eiflmrj5)

See it matching on regex 101.

It looks like a better explanation of lookaheads would be useful. You can think of them as asserting a regex from a given position in the string. So, lets define the positions in the string abc:

|a|b|c|
1 2 3 4

You can see that there are 4 positions in the string. Now, try to apply the lookahead (?=b).

|a|b|c|
^
Current position

No match, keep going.

|a|b|c|
  ^
  Current position

Got it, we just matched that from the current position, we get b. Now, we evaluate the rest of the original regex, starting from this position.

nickb
  • 59,313
  • 13
  • 108
  • 143
  • I used this http://stackoverflow.com/questions/469913/regular-expressions-is-there-an-and-operator to learn about the AND condition in Regex. According to the link, my regex looks correct. – John Rambo Feb 23 '16 at 19:53
  • @JohnRambo That's used to match two conditions at the same index into the string. You don't have two things at the **same** index, the strings you are looking for are at different positions in the string. – nickb Feb 23 '16 at 19:55
1

It is impossible for both (?=([0-9]+\s+ok\s+[0-9]+\s+nok)) and (?=(ltelidt|eiflmrj5)) to match at the same time. Perhaps you might want this instead:

([0-9]+\s+ok\s+[0-9]+\s+nok).*(ltelidt|eiflmrj5)

This says that, there could be any thing between condition1 and condition2.

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
bl4cksta
  • 794
  • 12
  • 32
1

If you really want to use positive lookahead ?=, then you should use the following regular expression:

(?=([0-9]+\s+ok\s+[0-9]+\s+nok)).* (?=(ltelidt|eiflmrj5))

Consider this regex, q(?=u) it matches a q that is followed by a u, without making the u part of the match. The positive lookahead construct is a pair of parentheses, with the opening parenthesis followed by a question mark and an equals sign.

Your regex was not working because your condition2 was not immediately after condition1. There were other characters present between condition1 and condition2. That's why you have to use .*

However, I would suggest you to avoid look ahead in your case, as it is not required.

http://www.regular-expressions.info/lookaround.html

Touchstone
  • 5,575
  • 7
  • 41
  • 48