Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
A quick quest: We want the result looks like this; from aaa---bbb-ccc into aaa.bbb.ccc

What I have tried:

Groovy
def objective = "aaa---bbb-ccc"
objective.replace(/.*[-]+.*/, '.')
How do we create a proper pattern of Regular expression in Groovy?
Posted
Updated 22-May-22 3:02am
v2

Did you tried this ?
def objective = "aaa---bbb-ccc"
objective.replace(/[-]+/, '.')


Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
Comments
Phoenix Liveon 15-Apr-22 23:21pm    
Yes, but it still not working.
Patrice T 15-Apr-22 23:48pm    
What result do you get ?
Phoenix Liveon 16-Apr-22 0:08am    
It still has the same output. "aaa---bbb-ccc"
That's because you don't use the "non-hyphen" data in your replacement.
Try this:
(.*?)-+(.+?)-(.*)

And this replacement string:
$1.$2.$3
 
Share this answer
 
Comments
Phoenix Liveon 16-Apr-22 1:42am    
It still not working?
def objective = "aaa---bbb-c"
objective.replace(/(.*?)-+(.+?)-(.*)/, '.')


OR either this one:
 objective.replace(/(.*?)-+(.+?)-(.*)/, '$1.$2.$3') 


if this is working, We would like to have a dynamic pattern.
Phoenix Liveon 16-Apr-22 1:45am    
We are really grateful for the responses that we have here. We're still searching for it.
OriginalGriff 16-Apr-22 2:04am    
It works for me:
var x = "123---abc-d";
var y = x.replace(/(.*?)-+(.+?)-(.*)/, '$1.$2.$3');
alert(y);

I get "123.abc.d"
You do realize that the replace functions doesn't modify the original string, but returns a new, changed string?
Phoenix Liveon 16-Apr-22 2:11am    
Yes, but the output is not we expected to be.
println( objective.replace(/(.*?)-+(.+?)-(.*)/, '$1.$2.$3') )
I use "println" in groovy. But the javascript you define is valid but in groovy when I println it, it did not.
Phoenix Liveon 16-Apr-22 2:15am    
I will try to store it to a name variable first, then print it to the console "println". pls wait for my test output. For some reason the IDE stuck at boot logo.
I found some answer and this is what its looks like we substitute method "replace" with "replaceALL";
def objective = "aaa---bbb-c"
println( objective.replaceAll(/[-]+/, '.') )
 
Share this answer
 
v2
Comments
Phoenix Liveon 16-Apr-22 5:20am    
Then we could do this;
def objective = "---aaa---bbb-c--"
objective.replaceAll(/[-]+/, '.').replaceAll( /^[\.]+(?=)|(?<=)[\.]+$/, '')

or do this;
def objective = "----.,,,aaa-,,,, ...--bbb-,.. c--   --"
println( objective.replaceAll(/[- \.,]+/, '.').replaceAll( /^[ \.,]+(?=)|(?<=)[ \.,]+$/, '') )
Phoenix Liveon 16-Apr-22 5:30am    
How do we group this one? /^[ \.,]+(?=)|(?<=)[ \.,]+$/ into ...Copy Code
/^([ \.,]+)(?=)|(?<=)\1$/
ORCopy Code
/^(?<name>[ \.,]+)(?=)|(?<=)\name$/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900