-2

I'm trying to match strings that appear either beetween : and ; or between : and end of string

For example

text: extract this; text: also extract this

I'm using expression (?<=:).*?(?=;) which captures the first one but can't figure out how to add between : and end of string

vaeinoe
  • 125
  • 6

1 Answers1

0

You can exclude the ; from the match:

(?<=:)[^;]+

Details:

  • (?<=:): positive lookbehind, the one you are already using
  • [^;]+: any character excepted a ;, between one and unlimited repetitions

https://regex101.com/r/Ib3MQn/5

Edit: VLAZ mentionned it in the comments below, please note that this regex will match multilines strings, ie this will match:

text: extract this; text: also extract this
and this will be extracted too
Max Xapi
  • 750
  • 8
  • 22
  • 1
    There is no need for the lookahead as the pattern is already going to avoid it. Although this *might* capture a newline as `[^;]` will match the end of line and continue: `a:b\nc` matches `b\nc` – VLAZ Nov 16 '20 at 10:23
  • Indeed you're right. Because the question is closed, i'm gonna delete this answer – Max Xapi Nov 16 '20 at 10:28
  • Too late! Is there a way to delete it? – Max Xapi Nov 16 '20 at 10:34
  • 1
    Not after it's accepted, I believe. Unless it gets unaccepted or a diamond mod deletes it but the latter happens only with rather big problems (e.g, plagiarism or spam). I'd suggest just editing the information to say how it behaves with multiline strings. If you wish, you can also give a multiline solution but it's not required. – VLAZ Nov 16 '20 at 10:36
  • 1
    You can omit this part `(?=;?)` as the `;` is optional in the pattern. – The fourth bird Nov 16 '20 at 11:29