Dealing with \ when saving a value in a check

Hi All,
Anyone know the regex expression to treat \ as a literal. I thought it was \ but this does appear to be working. Here is an example:

This is what is returned in the response:

/prefix/1234567789",

I’m using the following check to extract 123456789 and save to a parameter:

.check(regex("""/prefix/(.+)\",""").saveAs(“SavedValue”))

When checking the value of SavedValue in the session it appears as 123456789".

I don’t want the last two characters.

Many thanks

If you only want numbers in expected value, you could try something like

regex("""/prefix/\d+""")

“\d+” is suppose to give you only numeric values.

Many thanks Abhinav. I should have been more clear on the example I provided. The real value returned is alpha-numeric and also includes hyphens.

This seems to be working on regex tester page https://www.regex101.com/

`
Test string - /prefix/abc-1234567789"

prefix/(.+)\"
`

Many thanks for your help.

What you are really asking is, how do you properly put the backslash into the regex. That is a very good question that you need to know the answer to:

First, in order to specify a literal backslash in a regex, you must escape it with a backslash. : \ - so far so good.

Second, you are putting that in a string, which also uses a backslash as an escape. So the compiler is quite possibly converting that to just one backslash.

In order to have it make it into the regex as two backslashes, you may have to escape them both. So it becomes \\ - yes, 4 backslashes.

That said, the smart thing to do is accept only those characters that you expect: /prefix/([-a-zA-Z0-9]*)

And if you put the two together, you can be assured that the application is returning valid values in the responses:

/previx/([-a-zA-Z0-9]*)\\"

Hope that helps.