cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1011
Views
0
Helpful
5
Replies

String.replace regular expressions

robcav86
Level 1
Level 1

Can we use regular expressions in the string.replace method? For example:

 

strCallingNumber.replace("/'[+]?[9]?[+]?[1]?(\d{3})?(\d{3})(\d{4})|([135]\d{4})$/","$1-$2-$3$4")

 

This doesn't seem to do it, I tried without the quotes as well... 

1 Accepted Solution

Accepted Solutions

string.replaceAll(pattern as string,replacement as string) does not want the slashes on the regular expression pattern. The following gives me what I want, or at least expected

 

strTelNumber.replaceAll("[+]?[9]?[+]?[1]?(\d{3})?(\d{3})(\d{4})|([135]\d{4})$","$1-$2-$3$4")

View solution in original post

5 Replies 5

balaji.bandi
Hall of Fame
Hall of Fame

With this we would not be able to define what is your goal

 

can you explain, what is your input and what you expect to replace ?

 

or just use this website give you so many combinations to test

 

https://regex101.com/

 

 

 

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

To reformat a string to phone number. External calls come in as 918881231234 and internal as 3XXXX

 

I want external to be formatted as 888-123-1234 and internal to remain as is 31234

 

Thats actually the site I used to build the expression and it works as far as I can tell

string.replaceAll(pattern as string,replacement as string) does not want the slashes on the regular expression pattern. The following gives me what I want, or at least expected

 

strTelNumber.replaceAll("[+]?[9]?[+]?[1]?(\d{3})?(\d{3})(\d{4})|([135]\d{4})$","$1-$2-$3$4")

strCallingNumber.replaceAll("/[+]?[9]?[+]?[1]?(\d{3})?(\d{3})(\d{4})|([135]\d{4})$/","$1-$2-$3$4")

 

Input 918882223333

Desired output 888-222-3333

 

Input 31111

Desired output 31111

 

Am I going about this wrong?