Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match a pattern only once

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?

like image 705
wawiwa Avatar asked Mar 13 '13 17:03

wawiwa


1 Answers

You could use the following pattern (as long as your strings are always formatted the way you said) :

^\w+(?=-.*\.bla)

Regular expression image

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.

like image 81
Hugo Dozois Avatar answered Sep 30 '22 13:09

Hugo Dozois