0

I'm trying to match strings between whitespaces, but not have the whitespaces included in the results. For example:

Hi, my name is Tudor. should match my, name and is. My regex is:

(?<=\s){token}(?=\s)

I'm also curious why using

(?:\s){token}(?:\s)

Still keeps the whitespaces in the matches.

So far regex 1 seems to work fine, but I'm wondering if it's correct for all cases since I'm not a regex expert by any means.

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • Because the latter consumes spaces like token itself and the former leaves them alone though asserting them. BTW your working regex is what you want. – revo Feb 01 '19 at 12:02
  • Do you also want to match the first word in a line when it is followed by a whitespace? I just mean to say...are you referring only to spaces or whitespaces which include newlines too? [`(?<=\s)\w+(?=\s)`](https://regex101.com/r/q6OToH/1) vs [`(?<= )\w+(?= )`](https://regex101.com/r/q6OToH/2) – Gurmanjot Singh Feb 01 '19 at 12:02

0 Answers0