Someone please help me merge these two expressions PRCE


$CardText = preg_replace_callback('/\\<([twubrgx0-9]|hbg)>/i', 'ConvertCastingCost', $CardText);
	$CardText = preg_replace_callback('/\\<([twubrgx0-9]|hbg)/i', 'ConvertCastingCost', $CardText);

Basically, there may or may not be a closing > on the tag. Sloppy, I know, but I have to keep this backwards compatible.

/\<([twubrgx0-9]|hbg)\>?/i

should do it, I believe.

Yep. Thanks.

The backslash in the pattern isn’t doing anything at all; [COLOR="#A52A2A"]\\<[/COLOR] and [COLOR="#A52A2A"]<[/COLOR] will match exactly the same thing. The same point goes for the backslash that Tom introduced. The backslash is used to revoke any special meaning that so called metacharacters ([COLOR="#A52A2A"][[/COLOR], [COLOR="#A52A2A"]*[/COLOR], etc.) have, so that their literal characters can be matched. The angle bracket is not special*.

Michael, you don’t mention whether the callback function makes any use of the whole match or just the captured group. If the full match isn’t used, then there isn’t any need to even try to match the closing angle bracket, since it is optional.

* It can be, if being used to delimit the pattern. But that’s not the case here.

Good to know. Without doubt, expressions are my weakest area as a programmer.

The same point goes for the backslash that Tom introduced. The backslash is used to revoke any special meaning that so called metacharacters ([COLOR="#A52A2A"][[/COLOR], [COLOR="#A52A2A"]*[/COLOR], etc.) have, so that their literal characters can be matched. The angle bracket is not special*.

Ok.

Michael, you don’t mention whether the callback function makes any use of the whole match or just the captured group.

Just the captured group.

If the full match isn’t used, then there isn’t any need to even try to match the closing angle bracket, since it is optional.

Optional yes, but if it’s present I want to remove it when the replace occurs. If it where up to me I’d require it for consistency, but for historical reasons (and I can’t ask the product department to go back and redo some 30,000 entries) I have to make the expression tolerate the lack of a closing bracket.

Indeed, but since regex is based on arbitrary characters, it’s difficult to keep track of exactly which ones are special. I always find it more readable to escape all non-alphanumeric literals because it highlights that it’s a literal and not a character with special meaning :slight_smile:

I guess that’s easier than sitting down and learning. (:

Really, the list of “special” characters is very small: you probably know more array_*() functions.

My problem with regex is I’m mildly dyslexic. While I can muddle through most aspects of coding despite that liability (and IDE var and function hinting has been a God send), regex patterns are a real PITA for me.

A while back regex was also PITA for me. I couldn’t really make out much of what the PHP Manual said about them. But I found a great tutorial that seemed to explained regex in an easy enough way for me to follow. The tutorial isn’t online anymore but it can be found in the web archive, there’s also a copy of it on some obscure Chinese (I think) [URL=“http://wow730.wordpress.com/category/phptext-processing/”]blog. I went though it step by step and eventually learned it. Sure, sometimes I may forget a meaning of some character but now it’s just a matter of quickly looking up the PHP Manual to get me going in trouble.

I don’t regret it at all because regex are a huge time-saver whenever I need to parse some text, validate input, split string into components or do some intelligent find/replace in a regex-capable editor.