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

**URL:** <https://forum.robotframework.org/t/i-would-like-to-replace-the-symbol-in-a-numerical-string-using-python-code/6267>\
**Category:** Robot Framework\
**Created:** [28 September 2023 13:38 UTC](https://forum.robotframework.org/t/i-would-like-to-replace-the-symbol-in-a-numerical-string-using-python-code/6267 "2023-09-28T13:38:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AGC](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@AGC](https://forum.robotframework.org/u/AGC)\
**Post date:** [28 September 2023 13:38 UTC](https://forum.robotframework.org/t/i-would-like-to-replace-the-symbol-in-a-numerical-string-using-python-code/6267/1 "2023-09-28T13:38:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![HelioGuilherme66](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/helioguilherme66/32/34_2.png) [@HelioGuilherme66](https://forum.robotframework.org/u/HelioGuilherme66)\
**Post date:** [28 September 2023 15:41 UTC](https://forum.robotframework.org/t/i-would-like-to-replace-the-symbol-in-a-numerical-string-using-python-code/6267/2 "2023-09-28T15:41:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [28 September 2023 15:51 UTC](https://forum.robotframework.org/t/i-would-like-to-replace-the-symbol-in-a-numerical-string-using-python-code/6267/3 "2023-09-28T15:51:53Z")

</div>

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.

```plaintext
***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.

---

<div class="post-metadata">

**Author:** ![Jirawat-kumsiri](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/jirawat-kumsiri/32/3042_2.png) [@Jirawat-kumsiri](https://forum.robotframework.org/u/Jirawat-kumsiri)\
**Post date:** [28 September 2023 17:26 UTC](https://forum.robotframework.org/t/i-would-like-to-replace-the-symbol-in-a-numerical-string-using-python-code/6267/4 "2023-09-28T17:26:53Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![AGC](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@AGC](https://forum.robotframework.org/u/AGC)\
**Post date:** [29 September 2023 10:06 UTC](https://forum.robotframework.org/t/i-would-like-to-replace-the-symbol-in-a-numerical-string-using-python-code/6267/5 "2023-09-29T10:06:57Z")

</div>

Hi [Guilherme](https://forum.robotframework.org/u/HelioGuilherme66) Guilherme, It works, Many thanks

---

<div class="post-metadata">

**Author:** ![AGC](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@AGC](https://forum.robotframework.org/u/AGC)\
**Post date:** [29 September 2023 10:09 UTC](https://forum.robotframework.org/t/i-would-like-to-replace-the-symbol-in-a-numerical-string-using-python-code/6267/6 "2023-09-29T10:09:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AGC](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@AGC](https://forum.robotframework.org/u/AGC)\
**Post date:** [29 September 2023 10:12 UTC](https://forum.robotframework.org/t/i-would-like-to-replace-the-symbol-in-a-numerical-string-using-python-code/6267/7 "2023-09-29T10:12:16Z")

</div>

Hi [Jirawat-kumsiri](https://forum.robotframework.org/u/Jirawat-kumsiri) Jirawat Many thanks for your answer, have a nice day, and thanks again.
