Terenz

Regex digits with different symbol

4 posts in this topic

#1 ·  Posted (edited)

Hello, my basic strings:

(ABC - 9.00 x)
(ABC-12:00)
(ABC + 9.30 x)

My final result i'd like to have is:

-9
+12
+9.30

I need the decimal only if there are different from zero, i don't know if is possible with a regex this part or i need to do after. The symbol changes are + and - or . and :  for the digits, sometimes there are spaces. Thanks for the help with this

 

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

One way :

Local $aStrings = ["(ABC - 9.00 x)", "(ABC-12:00)", "(ABC + 9.30 x)", "(ABC - 22.35 x)", "(ABC + 07:02 x)", "(ABC + 14.00 x)"]

For $i = 0 To UBound($aStrings) - 1
    $number = StringRegExpReplace($aStrings[$i], ".*([+-])\h*(\d+)(?=[.:]\d{2})(?|([.:][^0]\d)|([.:]\d[^0])|[.:]00()).*", "$1$2$3")
    ConsoleWrite($aStrings[$i] & @TAB & " => " & @TAB & $number & @CRLF)
Next

 

Edited by jguinch

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Woa @jguinch really cool! I need just one step more, i did't notice yesterday but exist a chance that string is without numbers:

(ABC)

It that case i need to return 0. Many thanks!

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now