0

I want to create an expression which will select the first word immediately preceding every equals sign.

For example, take this sentence

"execute code='hello I am a program'"

In the example above, I just the word 'code', since it is the first word to precede an equals sign

The closest I've come to it is [^\s]+(?:[=]), which selects 'code='

If the sentence were "execute code = 'hello I am a program'" (all I did was add spaces)

then the selection we want would be 'code' still, but my current expression wouldn't find anything

I've tried other expressions that are also partially correct, such as [a-zA-Z]+[^=]

This one does would print out 'execute code' and 'execute code ' for both the cases above, respectively.

Please help, I cannot figure this out

Santi
  • 381
  • 1
  • 3
  • 7

1 Answers1

2

Take a look at lookahead assertions. It looks like you want a word ([^\s=]+) which is followed by some optional whitespace (\s*) and a '=', which would come out like [^\s=]+(?=\s*=)

manveti
  • 1,691
  • 2
  • 13
  • 16
  • This works! same answer as wiktor stribizew above. Thanks you both! – Santi Jan 30 '19 at 21:58
  • 1
    I updated this to incorporate his comment about `\S` vs `[^\s=]`, as my original would run into problems with input like "foo=bar=baz". It may not be an issue in practice, but better safe than sorry. – manveti Jan 30 '19 at 22:00