4

I'm trying to replace any group of two or more periods with just a single period. I suspect the + operator is involved, but I've had nothing but sorrow trying to make the expression using that... So I thought as an experiment I would try to replace just 3 periods with one period. The nonsense below is what I came up with, and of course it doesn't work.

OutNameNoExt:= RegExReplace(OutNameNoExt,"\.\.\." , ".")

Or even better, can I alter this existing expression

OutNameNoExt:= RegExReplace(OutNameNoExt,"[^a-zA-Z0=9_-]" , ".")

so that it never produces more than one period in a row?

Help?

Asaph
  • 159,146
  • 25
  • 197
  • 199
dwilbank
  • 2,470
  • 2
  • 26
  • 37

2 Answers2

7
OutNameNoExt:= RegExReplace(OutNameNoExt,"\.{2,}" , ".")

Or, if the {n,m} (i.e., at least n, but no more than m times) syntax is not allowed, you can use the following instead:

OutNameNoExt:= RegExReplace(OutNameNoExt,"\.\.+" , ".")

Alternatively, you can also change the existing expression to the following so that it doesn't produce more than one period in a row:

OutNameNoExt:= RegExReplace(OutNameNoExt,"[^a-zA-Z0=9_-]+" , ".")
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • Thanks sir! It works. Now I'll go refresh myself on what those curly braces mean. – dwilbank Aug 18 '12 at 23:54
  • @user1544566: You're welcome. `X{n,m}` basically means *match X, at least n but not more than m times*. When you omit the value of `m`, it means the expression is unbounded. For example, `\.{2,}` means *match . at least 2 times*. Here's a link that explains regex basic constructs: http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html. – João Silva Aug 18 '12 at 23:56
2

For Java, the following regex is working to replace multiple dots with single dot:

String str = "-.-..-...-.-.--..-k....k...k..k.k-.-";
str.replaceAll("\\.\\.+", ".")

Output:

-.-.-.-.-.--.-k.k.k.k.k-.-
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Mahesh
  • 21
  • 1