I would like to replace the symbol (-) in a numerical string, using Python code

Hi Everyone,
I need to replace the symbol (-) in a numerical string, for example from full_number=123456-987654321 to full_number=123456987654321
If I use the following code, it works perfectly, I receive the correct answer: full_number=123456987654321
[Arguments] ${full_number}=random

IF    "${full_number}" == "random"
    ${full_number}    get random bank account number
ELSE
    ${full_number}     replace string using regexp    ${full_number}    -    ${EMPTY}
END

full_number=123456987654321

but now if I want to use Python code for example re.sub(r"(\d+)-(\d+)“, r”\1\2", str(${full_number}))
it does not work, any idea please?
How would be possible usig python code to get 123456987654321 without the symbol (-)
I have used this code> re.sub(r"(\d+)-(\d+)“, r”\1\2", str(${full_number}))

IF    "${full_number}" == "random"
    ${full_number}    get random bank account number
ELSE
    ${full_number}    replace string using regexp    ${full_number}    (\d+)-(\d+)    \\1\\2
END

full_number=123456-987654321

I have used also this EVALUATE re.sub(r"(\d+)-(\d+)“, r”\1\2", str(${full_number})) but it does not work
Any idea? many thanks in advance.

If full_number='123456-987654321' is a string, then you can have:
full_number=full_number.replace('-','')

Hi Alejandro,

In your first “working” example, you didn’t need to use replace string using regexp as Replace String is sufficient.

As for the regex, you need to escape the escape characters (for Robot framework and then again for python, so it becomes a game of escape the escape characters for the escape characters

Also you can use Evaluate to call single line python commands directly, this is sometimes useful, especially if there is no robot framework library for what you are trying to acheive, so I’ve included examples of that too.

*** Settings ***
Library    String

*** Variables ***
${full_number} 	123456-987654321

*** Test Cases ***
Alejandro Simple String
	Should Be Equal As Strings	${full_number} 	123456-987654321
	${full_number}= 	Replace String 	${full_number} 	- 	${EMPTY}
	Should Be Equal As Numbers    ${full_number}    123456987654321

Alejandro Regex Example
	Should Be Equal As Strings	${full_number} 	123456-987654321
	${full_number}=    replace string using regexp    ${full_number}    (\\d+)-(\\d+)    \\1\\2		count=2
	Should Be Equal As Numbers    ${full_number}    123456987654321

Alejandro Python Method
	[Documentation]		This method doesnt need String Library
	Should Be Equal As Strings	${full_number} 	123456-987654321
	${full_number}= 	Evaluate 	${full_number.replace("-", "")}
	Should Be Equal As Numbers    ${full_number}    123456987654321

Alejandro Python RE Method
	[Documentation]		This method doesn't need String Library
	Should Be Equal As Strings	${full_number} 	123456-987654321
	${full_number}= 	Evaluate 	re.sub("(\\d+)-(\\d+)", "\\\\1\\\\2", "${full_number}")		modules=re
	Should Be Equal As Numbers    ${full_number}    123456987654321

Hope this helps,

Dave.

you can use Remove string using regexp to remove every thing except number
*** Settings ***
Library String

*** Test Cases ***
Extract numbers
${input_string} Set Variable 123456-987654321
${numbers_only} String.Remove string using regexp ${input_string} [^0-9]
Log to console ${numbers_only}

output //123456987654321

2 Likes

Hi Guilherme Guilherme, It works, Many thanks

Hi Dave, Many thanks for all your explanations and examples, I have tested all of them, and they work. Many thanks

1 Like

Hi Jirawat-kumsiri Jirawat Many thanks for your answer, have a nice day, and thanks again.