Regex issue: Special characters in string variable

Ask general questions here.
mrt
Posts: 88
Joined: Mon Mar 16, 2020 11:31 am

Regex issue: Special characters in string variable

Post by mrt » Thu Apr 29, 2021 12:56 pm

Hey folks,

I have to identify an element by its innertext string from a webpage.
The string is read from a variable read from a data source.

The issue is, because of angular rendered page, that each string contains a different amount of leading and trailing whitespaces and linebreaks, in browser console it looks like this (i have entered dots for whitespaces so you can see it):

Code: Select all

<div id='label'>
"..
...........(A) This is some text..
........."
</div>
My variable contains:

Code: Select all

$mylabel="(A) This is some text"
So I cannot use exact match
innertext = $mylabel
because of the whitespaces,
and i also cannot use regex match
innertext ~ $mylabel
because it won't find the element.

But on other elements with same structure, the regex match works as expected.
The only difference I could notice, are the brackets inside the string.

When investigating, I found out by hardcoding:
1. innertext~"A"
2. innertext~"This is some text"
3. innertext~"(A"
1. and 2. work,
3. does not work.

But unfortunately its not sufficient to enter only parts of the string, I need the full text from the data source.

So I assume, because of the '~' for regex match, and the brackets inside the string variable, the brackets are recognized and interpreted as some regex character and therefore identification fails?
If this is true, I cannot escape single characters like the brackets inside the data source variable.

Do you have any idea how I could get this identification to match?

thanks!

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 6544
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Regex issue: Special characters in string variable

Post by odklizec » Thu Apr 29, 2021 1:06 pm

Hi,

Have you tried to "escape" parenthesis directly in data source? This should do the trick...

Code: Select all

\(A\) This is some text
But I understand, it's a pain to modify data source in such way and it could have some unwelcome side effects as well ;) But it's probably the simplest solution?

If you don't want to modify data source, you can probably use the solution mentioned here:
viewtopic.php?f=9&t=5050&p=21243#p21235
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

mrt
Posts: 88
Joined: Mon Mar 16, 2020 11:31 am

Re: Regex issue: Special characters in string variable

Post by mrt » Thu Apr 29, 2021 1:22 pm

Hey Pavel,

yes, modifying the data source is what I want to avoid - also because the data source is filled by other colleagues and teaching them to remember, recognize and then manually escape special characters is not what I am looking for.

Thank you:
1. for you blazingly fast answer
2. for the great solution:
Regex.Escape
does exactly what I was looking for, works great, thanks!

BR mrt