The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Matching pattern then*
Discuss Matching pattern then* in the Regex Programming forum on Dev Shed. Matching pattern then* Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 11th, 2009, 09:32 PM
|
|
|
|
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:
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*

|

November 12th, 2009, 09:24 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
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.
|

November 12th, 2009, 05:23 PM
|
|
|
Quote: | 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.
|

November 16th, 2009, 08:33 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|