...

View Full Version : Fairly easy regular expression help



arnyinc
11-21-2007, 08:36 PM
I'm trying to match something that I think should be pretty simple. I want to match everything between two hashmarks. I think I am having problems with the newline characters specifically.

"##I want to match
this text##"

Here is my php code:



preg_match_all('/(##[.*\s]*##)/', $mytext, $myArray);

Mwnciau
11-21-2007, 09:32 PM
preg_match_all('/(\#\#.*\#\#)/is', $mytext, $myArray);

arnyinc
11-21-2007, 09:53 PM
Cool, thank you. That got me 75% of the way there since you reminded me of the modifiers.

It still isn't matching my text but I suspect that is due to the other characters I have in there. I need it to match '@', '=', apostrophe, and a single '#'. I'll just need to figure out which of these need to be escaped.

arnyinc
11-26-2007, 02:44 PM
Can anyone help further with this regex question? I can't get my expression to match the text I am looking for.



$retrievedText = "##test1## ##test2##";
preg_match_all('/(\#\#.*\#\#)/is', $retrievedText, $commandArray);

print_r($commandArray);


Output: Array ( [0] => Array ( [0] => ##test1## ##test2## ) [1] => Array ( [0] => ##test1## ##test2## ) )

Ideally I want "test1" and "test2" returned. Or at least "##test1##" and "##test2##".

I've tried spraying random [] and () around the hash marks and the .* but I haven't figured out the correct way yet.

Inigoesdr
11-26-2007, 08:41 PM
$retrievedText = "##test1## ##test2##";
preg_match_all('/##(.*?)##/is', $retrievedText, $commandArray);
print_r($commandArray[1]);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum