Regex question regarding single character
I would like to use regex to replace a single character in a URL.
The url is below:
http://www.example.com/e343/Product.html
I would like to use regex to replace any character that is not a "t"
after the first slash. In this case the letter being used is the letter "e"
In this case I am using ColdFusion MX so if there is an alternative to reg开发者_开发问答ex I'll be happy to use it.
([^/]+//[^/]+/)([^t])
$1 = before your inalid character
$2 = the char 'e', ot anything other than 't'
Based on your parser you may need to escape / chars to \/
You can use the REReplace function in CF MX for this. The basic idea to to make a regex that puts everything except the character you want to replace into capture groups, and use the backreferences to keep them in the replacement string. Something like:
REReplace(url, "(^http://[^/]*/)([^t])(.*)", "\1t\3", "ONE")
If the first character after your first / is a t, the entire regex won't match, and nothing will get replaced.
<cfset address = 'http://www.domain.com/e343/Product.html' />
<cfset updated = reReplaceNoCase(address, '(\w/)[^t]?', '\1') />
<cfoutput>#updated#</cfoutput>
Result
http://www.domain.com/343/Product.html
It always finds the first slash preceded by a letter, and also includes the letter after the slash if it is not a 't'. Then it replaces the match with everything except for the letter that is not 't'.
Here is the most useful regular expression page i have ever found. Every developer should link to this one.
http://txt2re.com/
精彩评论