Reputation: 371
Hi I need to match a character string which can have 0+ hyphens in between the characters. I'm using PHP preg-match to do this. The characters can be upper or lowercase. Here's a few examples:
TESTTESTtest published successfully
test-TEST-test published successfully
Test-test published successfully
My regex needs to capture all these different cases.
I am going through a log line by line and trying to match the regex below to each line.
This is my current if statement:
if(preg_match('/(\w*) published successfully/',$line,$matches){
}
I just used \w* as my character capture as I didn't need to worry about hyphens before but I need to adapt this to handle the new scenario. Can anyone help me out?
Upvotes: 0
Views: 158
Reputation: 626870
If you do not need to worry about edge cases like ----- published successfully
all you need is to add the \w
and -
to a character class [...]
(and I also suggest using a +
quantifier to match 1 or more occurrences, not 0 or more, to avoid getting empty matches):
if(preg_match('/([\w-]+) published successfully/',$line,$matches){
}
See the regex demo.
Upvotes: 1