cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
285
Views
1
Helpful
14
Replies

Expressway string replacement format

aalejo
Level 5
Level 5

Dear Community

I notice that on a replacement string \. or . ,both work the same.

For example:

Regex expression: (.*)@nono\.com

I can use replacement string: 1234@yesyes\.com OR 1234@yesyes.com

both will lead to same results.

If initial string is 1234@nono.com, both replacement strings with lead to 1234@yesyes.com


Any idea why?

 

 

14 Replies 14

A dot in Regex means any one character and since the dot is a character then it matches that in your replace that doesn’t have the escape character \ in front of the dot. If you want to be sure it would match only the literal string with the dot exactly in that position I would recommend you to use the match statement that has the escape character so that it literally matches a dot and nothing else.



Response Signature



SearchRule.png

 

Hey Roger
Thank you for your answer.

I am not taking about the regex pattern: i am taking about the replacement string that is configure after a regex is matched.  i get the behavior of \. and . on the regex pattern. (Pic attached)

In a nutshell replace string 1234@yesyes\.com  and 1234@yesyes.com both have same results. And they are clearly not the same.

i think the correct one should be \., because it is a regular expression that is the literal " ." If we use   " ." , it means to replace for any character, and will not make sense (to me). Thats why i was surprise that , in practice, both do the same when use on Replace string.


I’m afraid that you lost me, I’m not really grasping your question. Hopefully someone else in the community will.



Response Signature


Hey Roger

Look to Replace string in both of my examples. One is 1234@yesyes\.com  and  the other is 1234@yesyes.com . But both create same end result. Why?

Alex

In my view I already have explained that, but you seem to disagree. It’s irrelevant that I explained it from the match perspective, it operates in the same way on both sides.



Response Signature


Hi Roger

Stay with me here.

You said "A dot in Regex means" but the dot i am referring to is on the  Replace string as it is shown in the picture. Replace string can use regular expression (like \n)  to reference back to Regex string  but in this case there is no \n pattern. To where dot reference to when used on the replace string?

I do understand what the dot on Regex string reference to and how can be use.
Alex

Again in my view I’ve provided how I think this works. But as you see it differently I don’t know what I should do to make it across in a way that you feel is appropriate.



Response Signature


Hey Roger

Thank you for your responses anyways!
Will continue looking around

Have a nice weekend
Alex

Maybe this could make my point come across. Both the strings, Pattern String (Match) and Replace String operate with Regex.

image.png

Hopefully that helps.



Response Signature


Hey Roger

I get that. The point is: What a regular expression reference back when use on pattern string or on replace string?.

To be more precise i am using the naming that Expressway uses on a testing rule.

A regular expression on a Pattern String  reference back to an Alias pattern and on a Replace String reference back to a pattern on a Pattern String.

On this example, When you enter (.*) on pattern string you are reference something on the Alias (in this case 1234). If you enter \1 on the Replace String you are referencing back to a (...) on  a Pattern string (that also could reference back to something on Alias).

In  this case my question is where dot on replace string reference back on Pattern String.
Alex.

I think you’re overthinking this and make it more complicated than it is.



Response Signature


As @Roger Kallberg has said, in Regex, just a straight "." refers to any standard ASCII character, whereas a "\." refers specifically to the dot character.   So a match for a "." will match anything, including the dot (ie, it will also match, from your example, 1234@yesyesXcom).  If you want to match only with 1234@yesyes.com then you should use the \. in the pattern to enforce that character between yesyes and com is a dot and not any other character.  The replace string does not need a "." to be escaped with the "\" as the pattern string does, as it is a 1 for 1 replacement.  You shouldn't be using a "\" in a replacement string, unless you want to include a result from a previous match, ie the \1, \2, \3, etc will be anything that is included within brackets in the pattern string (a group construct), in the order the brackets appear if there are multiple groups.   Noting that none of your screenshot examples include such a thing, so none of your replacements should include a "\".
If you want to play around and test Regex and check results of matches, the regex101: build, test, and debug regex site is a good one to use.

Wayne

Please remember to mark helpful responses and to set your question as answered if appropriate.

Hey Wayne

What i am looking is \dot on the replacement string and not on the pattern matching string. Indeed \dot can be use on the replacement string (you can test it on expressway). 

Searching and asking around i found the answer to my own question here:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/substitutions-in-regular-expressions
"The replacement pattern can consist of one or more substitutions along with literal characters"

Meaning that on a replacement string you can use replacement patterns (\1,\2,\3) and literals (like 'a','b','c' etc) and you can NOT use (.*) or d{3}  as regular expressions.

Since on replacemnet string you can use ONLY replacement patterns and literals and  \dot is not a replacement pattern, \dot is also a literal (the literal dot). Then \dot = dot on replacement pattern but \dot <> dot on matching pattern.

Thats solves the mystery!

Thanks for your answer.

Alex

SearchRuleFinal.png