I have a string
foo-bar-bat.bla
I wish to match only foo
My flawed pattern matches both foo
and bar
\w+(?=-.*\.bla)
How do I discard bar
? Or maybe even better, how could I stop matching stuff after foo
?
You could use the following pattern (as long as your strings are always formatted the way you said) :
^\w+(?=-.*\.bla)
Edit live on Debuggex
The ^
sign matches the beginning of the string. And thus will take the very first match of the string.
The ?=
is meant to make sure the group following is not captured but is present.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With