2

I am looking for a RegExp to stop matching after a certain keyword.

This is my Input:
blabl

texta 1,40
textb 3.50
textc 6,90

blabal 2 3
ddd 3 jj d

key textd textf
2,30 4,70 5,90

What I want to match:
blabl

texta 1,40
textb 3.50
textc 6,90

blabal 2 3
ddd 3 jj d

key textd textf
2,30 4,70 5,90

What my RegExp currently looks like
[\d]{1,2}[.,][\d]{2}

What I tried to fix the problem
[\d]{1,2}[.,][\d]{2}(?=key)

So I was looking for something like this:
(What I want to match)(anything)(keyword)

I tried the following RegExp:
([\d]{1,2}[.,][\d]{2}(.|\n)+(?=key))
But now it matches:

texta 1,40
textb 3.50
textc 6,90

blabal 2 3
ddd 3 jj d

key textd textf
2,30 4,70 5,90

I read about the non-capturing groups, which seemed to solve my problem. So I tried:
([\d]{1,2}[.,][\d]{2}(?>(.|\n)+)(?=key))
But that does not match anything at all.

I feel like I'm almost there, but I could really use some help. Any idea what I'm missing is appreciated. If you have any questions regarding my problem, please do not hesitate to ask.

Thanks in advance for your time and effort.

Th0rne
  • 35
  • 5
  • 1
    First thing i would say is format your question. check this is this what you wanna achieve https://regex101.com/r/AbWHsW/1/ – Code Maniac Jan 10 '19 at 16:22
  • Thank you for your quick response. Is there a possibility to use the keyword "key" as an orientation as texta, textb, textc may vary? – Th0rne Jan 10 '19 at 16:27

1 Answers1

1

You may use

/\d+[,.]\d+(?=[\s\S]*key)/g

See the regex demo.

Details

  • \d+ - 1+ digits
  • [,.] - a , or .
  • \d+ - 1+ digits
  • (?=[\s\S]*key) - a positive lookahead that requires any 0+ chars followed with a word key immediately to the right after the last 1+ digits matched.

JS demo:

var s = "blabl\n\ntexta 1,40\ntextb 3.50\ntextc 6,90\n\nblabal 2 3\nddd 3 jj d \n\nkey textd textf\n2,30 4,70 5,90";
var rx = /\d+[,.]\d+(?=[\s\S]*key)/g;
console.log(s.match(rx));
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you so much! This is exactly what I was looking for. However I don't quite understand what the \b is for? – Th0rne Jan 10 '19 at 16:32
  • 1
    @Th0rne `\b` is a word boundary. `\b\d+` will match if the digits are preceded with a non-word char (a char other than an ASCII letter/digit or `_`) or start of string. `\bkey\b` also matches a whole word `key`, it should not be followed with a word char or end of string. If you do not need that behavior, you may remove `\b`. I assumed you need it judging by the sample string that has `key` as a whole word. I removed the word boundaries so as to focus on what is more important here. – Wiktor Stribiżew Jan 10 '19 at 16:33