Regex problem : match only the first occurence between two terms

Viewed 33

"Received money from SAYLOR SELLY EU S.A.R.L. with reference XXWW025 MENTINUI-NOM 3 654654465/WCX6565223323"
"Card transaction of EUR issued by CaniR* I- CKOUION"
"Card transaction of EUR issued by Suil Flim KL RUIOKE SOR"

I have these three lines and would match :

SAYLOR SELLY EU S.A.R.L.
CaniR* I- CKOUION
Suil Flim KL RUIOKE SOR

I was trying this and select the group 2:

(by|from|to)(.*)(with|\")

But it doesn't work. Any idea ?

1 Answers

As you mention awk in you comment, it does not support a non greedy quantifier .*?

What you might do is get the longest match first, and then split that match on the last part of the regex again and take the first item.

awk '
match($0,/(by |from |to )(.*)( with|")/, a) {
  split(a[2], b, /( with|")/)
  print b[1]
}
' file

Output

SAYLOR SELLY EU S.A.R.L.
CaniR* I- CKOUION
Suil Flim KL RUIOKE SOR
Related