0

I know that there are some similar questions that already have been answered, but from what I understood, they were too specific, in order to be of any use for my problem. I also thought this might be interesting to anybody facing a similar problem in the future since a possible solution to this problem will most probably not be able to rely on any special characters, but solely on alpha-numeric characters.

Link to the regex on regex101

If the link doesn't work, here's what I want to do:
I want to extract

Hello I want this to be matched World

from this string:

blablabla Hello blablablabla blablabla Hello blablabla blablabla blablabla Hello I want this to be matched World blablabla blablablablablablablablablablablabla

in the shortest possible fashion. The problem here is that my regex will match from the very first occurence of Hello, which results in

Hello blablablabla blablabla Hello blablabla blablabla blablabla Hello I want this to be matched World

My problem is I don't know how many Hellos might occur between the first Hello and the Hello right before I want this... Does anybody know an elegant solution for this problem? Thanks in advance!

Klaus Valse
  • 15
  • 1
  • 10
  • 1
    Use [`Hello(?:(?!Hello).)*?World`](https://regex101.com/r/5trQBZ/2). Or [`.*(Hello.*World)`](https://regex101.com/r/5trQBZ/3) – Wiktor Stribiżew May 28 '18 at 12:48
  • What do you mean by *I want to extract `Hello I want this to be matched World`*? If you already have the string what's the point of extracting it? do you want the position or do you want everything between latest `hello` and first `world`? – Mazdak May 28 '18 at 12:52
  • "hello", something, "World" without any "hello" between them. You should post it as answer @WiktorStribiżew – Cid May 28 '18 at 12:53
  • @Cid Those 2 have already been posted. No need to duplicate. – Wiktor Stribiżew May 28 '18 at 12:54
  • Thanks! Just to make sure I really understand the pattern: Hello(?:any string not containig Hello)repeat as many times as necessary World? Did I understand it correctly? It works in this simple example, and I reckon it'd also work with bigger streams. Thanks! – Klaus Valse May 28 '18 at 14:01
  • @WiktorStribiżew. I'm very sorry, but I have another question that takes this case a bit further. It'd be amazing if you could once again help with your regex expertise. I have this text: text startkeyword [1]: content. More text, another startkeyword [2]: content more text startkeyword [3]: WANTED CONTENT endkeyword. End of text I want to extract "WANTED CONTENT". Now it's pretty similar, to my previous question, but note that the digits are changing in the brackets which I could not resolve... Any ideas? Thanks for your help! – Klaus Valse Jun 05 '18 at 07:19
  • Try something, post a real question. – Wiktor Stribiżew Jun 05 '18 at 08:07
  • Alright. I thought it's bad practice, since this probably also qualifies as duplicate... – Klaus Valse Jun 05 '18 at 10:27

0 Answers0