#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    227
    Rep Power
    16

    Matching pattern then*


    I want to "replace with a space" all * characters in a string that are immediately following a group a characters which are not solely letters a-zA-Z - but allowing for leading [-] ie. -example*

    So the regex would NOT replace * in the following:

    Code:
    -example*
    example*
    because it is only preceeded by letters and an optional - character.

    But it WOULD replace the * in the following:

    Code:
    ex2amle*
    example2*
    2example*
    exa-mple*
    -examp2le*
    -2example*
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    You'll have to anchor the expression at the start of the string:
    Perl:
    s/^(\-?[a-z]+)\*/$1 /i

    PHP:
    preg_replace("/^(\-?[a-z]+)\*/i", "\\1 ", $theString);

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    227
    Rep Power
    16
    Originally Posted by ManiacDan
    You'll have to anchor the expression at the start of the string:
    Perl:
    s/^(\-?[a-z]+)\*/$1 /i

    PHP:
    preg_replace("/^(\-?[a-z]+)\*/i", "\\1 ", $theString);

    -Dan


    hi dan,

    that finds matches in a string starting with that sequence, but how would you change it to find that sequence "anywhere" in the string?

    ie. it could be at the start of the string, at the end of the string, or have a space either side.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Sorry, you want it to replace the * with a " " whenever the * follows a mixed group of letters and numbers? You can't do that with just a regular expression, the closest you can come is:
    preg_replace("/([a-z]*[0-9]+\w*)\*+/i", "\\1 ", $theString);

    That replaces everything in your example except for exa-mple, because it doesn't fit the rule you've given anyway. Example script:
    PHP Code:
    $strings = array(
    "-example*",
    "example*",
    "ex2amle*",
    "example2*",
    "2example*",
    "exa-mple*",
    "-examp2le*",
    "-2example*",
    "This is a long example, you should see no space here* but a space 3here* and -here2343* but not here*.");
    echo 
    "</pre>";
    foreach ( 
    $strings as $theString ) { 
        echo 
    preg_replace("/([a-z]*[0-9]+\w*)\*+/i""\\1 "$theString) . "<br />\n";

    Output:
    Code:
    -example*
    example*
    ex2amle 
    example2 
    2example 
    exa-mple*
    -examp2le 
    -2example 
    This is a long example, you should see no space here* but a space 3here and -here2343 but not here*.
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo