Re: Re: Space in regex

From: Date: Thu, 16 Nov 2006 22:26:19 +0000
Subject: Re: Re: Space in regex
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-244756@lists.php.net to get a copy of this message
At 11/16/2006 01:56 PM, Dotan Cohen wrote:
$text=preg_replace_callback('/\[([A-Za-z0-9\'.-:underscore:]+)\|([A-Za-z0-9\'. -:underscore:]+)\]/i' , "findLinks", $text); This regex should match any pair of square brackets, with two bits of text between them seperated by a pipe, like these: [Ety|wife] [Jarred|brother] [Ahmed|neighbor] [Gili and Gush|pets] [Bill Clinton|horny] I would expect that the "." would match spaces, but it doesn't. So the first three examples that I've shown are matched, but the last two are not. I've even added "\w", "\s", " ", and ":space:" to the regex, but of course that's not matching, either. What am I doing wrong? Note that I've been honing my regex skills for the past few days, but I've not so much experience with them. Thanks in advance to whoever can help me understand this.
Dotan, I'm surmising what you really want to do is grab all the characters between [ and | for the first field, and everything from | to ] as the second field. I would therefore identify the first field with:
        [^\]|]
        anything except the close-bracket and the vertical pipe
and the second field with:
        [^\]]
        anything except the close-bracket
Therefore:
        /\[([^\]|]+)\|([^\]]+)\]/
Regards, Paul

« previous php.general (#244756) next »