0

I am using PHP Regex to see if a pattern is in my string. Simply put, I want one of two words followed by a number. So, here's my pattern:

#(guitare|piano)[0-9]#

Basically, if the string contains the word "guitare", I don't want it to be matched, only if I have "guitare9" or "piano0"

At this point, If I use this pattern for the follwing string:

J'aime la guitare9

The array of the preg_match() returns guitare and guitare9: http://www.phpliveregex.com/p/7tZ

What do I have to change in my regex to only match guitare9 ?

1 Answer 1

1

Turn capturing group in your regex to non-capturing group. Because preg_match would display the match and also the captured strings in an array. By turning capturing group into non-capturing, you must get a single array element.

#(?:guitare|piano)[0-9]#

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reminding me, I had to wait 9 minutes and I forgot !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.