Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts

    Fairly easy regular expression help

    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:

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

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    preg_match_all('/(\#\#.*\#\#)/is'$mytext$myArray); 

  • #3
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Can anyone help further with this regex question? I can't get my expression to match the text I am looking for.

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

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    PHP Code:
    $retrievedText "##test1## ##test2##";
    preg_match_all('/##(.*?)##/is'$retrievedText$commandArray);
    print_r($commandArray[1]); 


  •  

    Posting Permissions

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