Home > Blockchain >  Regex Extraction - Match before a space, or NOT before a space
Regex Extraction - Match before a space, or NOT before a space

Time:02-11

Here are my potential inputs:

  1. [email protected], [email protected]
  2. [email protected], [email protected]

What I want to do is extract the @muck.co email address.

What I have tried is: \s.*@muck.co

The problem is that this only grabs an email address if it is preceded by a space (so it would only match the second example input above). . . How would I write a Regex expression to match either inputs?

CodePudding user response:

\s matches for a space, so you should wanted to use something like [^\s]*@muck.co - this means any number of not space caracters. [] - for a set of symbols, ^ - for negate effect.

It does not work for me, because \s in my regex flavour seems to not contain regular space, but this works [^[:space:]]\ @muck\.co. Also \ instead of * for one or more non-space characters instead of any number and escape dot \. which unescaped stands for any single character.

CodePudding user response:

You can use a negated character class to not cross the @ and use either a word boundary at the end to prevent a partial word match:

[^\s@] @muck\.co\b

Regex demo

  • Related