SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)

    Someone please help me merge these two expressions PRCE

    Code php:
    $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.

  2. #2
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    /\<([twubrgx0-9]|hbg)\>?/i

    should do it, I believe.

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Yep. Thanks.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    The backslash in the pattern isn't doing anything at all; \< and < 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 ([, *, 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.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    The backslash in the pattern isn't doing anything at all; \< and < will match exactly the same thing.
    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 ([, *, 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.

  6. #6
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Salathe View Post
    The backslash in the pattern isn't doing anything at all; \< and < 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 ([, *, etc.) have, so that their literal characters can be matched. The angle bracket is not special*.
    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

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomB View Post
    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
    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.
    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    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) 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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •