RegEx match word without a dash?

I got this sting that I would like to match in these anchor links I mean preg_replace but leave anchor text:


<a href="">Huelva</a>

<a href="">Huelva</a>

Don’t match if dash is found like this huelva-

Match if huelva?language_id=1



RegEX that seems to be working:

<a href="https:\/\/www\.mysite\.com\b(\/gente\/huelva)\b(?<!-)">([^>]*)<\/a>

Well, the pattern you’ve given does not match the links you’ve given, even if I fix the front end.

There is no \b present on the front end.

What you appear to be trying to do is:

<a href="https:\/\/www\.mysite\.com(\/gente\/huelva)(\?.*?)?">([^>]*?)<\/a>

Thanks buddy so this wont work:

preg_replace('#<a href=".*?\b(\/gente\/huelva)\b">([^>]*)<\/a>#i', '$2', '<a href="">Huelva</a>');

So the idea is to replace a single link with the text “/gente/huelva”?

preg_replace('#<a href=".*?\b(\/gente\/huelva)(\?.*?)?">([^<]*)<\/a>#i', '$2', '<a href="">Huelva</a>');

I’d like to replace it with Huelva anchor link text.

$3 then, cause we introduced a new subpattern.

OK thanks so much buddy for your support.

Did you know you can make matching groups non-capturing? Just start with ?:, i.e., (?:\/gente\/huelva)

That will match, but it will not be available in the replacements and it’s place will not be reserved.
Handy for when you need an capture group but you’re interested in what’s actually in there.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.