views:

79

answers:

4

I wrote a Regular Expression that generates a url like

/abc/deutschland/bbs-tagesfahrten/betz-mode-frotier-center-–-tress-teigwaren.html.

Now I want to replace the repeating dashes with the single on. How Can I?

+2  A: 

Use this:

s/---*/-/g
This will also replace "-" with "-". Harmless, but unnecessary.
Carl Smotricz
@Carl You are correct. Let me change that to `---*`.
@lutz Another solution is `s/--+*/-/g`
Gaim
@Gaim: You sure you want that asterisk in there? ;)
Carl Smotricz
+4  A: 
String.replaceAll("--+", "-");
Carl Smotricz
+1  A: 

To replace any repeated dashes in the whole URL:

<cfset InputUrl = "/abc/deutschland/bbs-tagesfahrten/betz-mode-frotier-center-–-tress-teigwaren.html">
<cfset CleanUrl = REReplace(InputUrl, "-+", "-", "ALL")>

To work on the file part only:

<cfset PathPart = REReplace(InputUrl, "(.*/).*", "\1")>
<cfset FilePart = ListLast(InputUrl, "/")>
<cfset CleanUrl = PathPart & REReplace(FilePart, "-+", "-", "ALL")>
Tomalak
+2  A: 

Perhaps simpler that any of the suggestions would be:

s/-{2,}/-/g
ternaryOperator
to CF-ize tO's suggestion:<cfset cleanURL = reReplace(InputURL,"-{2,}","-","all")>
marc esher
+1. Good one guys. That is a bit more intuitive as well.
Leigh