Solved

Substring or extrac last characters

  • 8 February 2024
  • 5 replies
  • 374 views

Badge +3

Hi, 
I have a string. The last characters of the string are numbers. I want to substring or extract them. These are some examples of input strings: 'xdfg8941332', 'dder899945566445', 'cfgnnff89'. I have used 'find' with the following regex: \d+, but it has not been successful.

icon

Best answer by Zaid Chougle 9 February 2024, 07:12

View original

5 replies

Userlevel 6
Badge +15

Hi @luis.espeleta ,

the regular expression r'\d+$' is used to match one or more digits (\d+) at the end of the string ($)

 

 

Badge +3

Thank you, I tried it and got this:
(str_Factura: XDGC992679)
Why does it show 0?

 

Userlevel 4
Badge +14

Thank you, I tried it and got this:
(str_Factura: XDGC992679)
Why does it show 0?

 

It is because the specified substring is not found. In your case you should use extract / substring. I believe you are using Find.

 
Userlevel 4
Badge +14

Hi @luis.espeleta ,

I got the result with below logic : 

The key thing is I passed the RegEx in a variable and then send it to replace as it contains $ and AA checks for variable declaration.

The code works in fashion :

  • List contains all the string where last numbers are to be extracted.
  • Initially I replaced the last digits from the variable using RegEx : \d+$
  • After that from the original string I removed the replaced text and got the desired Output.

Let me know if any query comes.

Userlevel 4
Badge +14

Hi @luis.espeleta ,

I got the result with below logic : 

The key thing is I passed the RegEx in a variable and then send it to replace as it contains $ and AA checks for variable declaration.

The code works in fashion :

  • List contains all the string where last numbers are to be extracted.
  • Initially I replaced the last digits from the variable using RegEx : \d+$
  • After that from the original string I removed the replaced text and got the desired Output.

Let me know if any query comes.

Sample String has the input string and Regex has : \d+$ and is stored in Test1

Sample String has the input string, Test1 has the string other than last digits and the result is stored in OutPut

 

Example : Using String Replace 

First Screenshot : 

Input :

  • SampleString : 12xd23fg8941332
  • Regex = \d+$

Output : 

  • Test1 = 12xd23fg

Second Screenshot

  • SampleString : 12xd23fg8941332
  • Test1 = 12xd23fg

Output : 

  • OutPut = 8941332

 

Reply