Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove any string from a variable
#1
Which command should I be looking at if I want to remove strings in a variable? For example, if the variable contains "Balance (100)" I just want it to return "100" where all the alphabet and everything else are removed?

Put in mind that the string can be different, not the same at all times. It can be "Net (400)", "Input (500)", etc.


Thank you
#2
Use replacerx or findrx.

Code:
Copy      Help
str s="Balance (100)"
s.replacerx(".+?(\d+).*" "$1" 4)
out s

Here ".+?(\d+).*" ir regular expression.
.+? is 1 or more any characters. Matches "Balance (".
\d+ is 1 or more digits. Matches "100".
() marks the part of the string that can be specified as $1 in the replacement string.
.* is 0 or more any characters. Matches ")".
#3
Thanks, that works out great!
#4
Code:
Copy      Help
str s="Balance is (100,323.22)"
s.replacerx(".+?(\d+).*" "$1" 4)
out s

I tried the code above and returned 100 instead of 100,323.22. What am I doing wrong?

Thanks!
Denise
#5
Replace

\d+

to

\d+(?:,\d+)*(?:\.\d+)?


Forum Jump:


Users browsing this thread: 1 Guest(s)