Jump to content

Regex help


Go to solution Solved by TheXman,

Recommended Posts

I need your Regex skils.
I need a number out of this, if the number is above the 1 then it works, but if it is below the 1 examle 0.96 it shows 96.
How can I fix this?

#include <StringConstants.au3>

Local $teststring1 = "(000.96*A)"; has to be 0.96
Local $teststring2 = "(001.96*A)"; has to be 1.96

Local $array = StringRegExp($teststring1, "\(0*\.*(.*)\*", $STR_REGEXPARRAYGLOBALMATCH, 1)
ConsoleWrite($array[0] & @CRLF)

 

Edited by nend
Link to comment
Share on other sites

  • Developers

Is there always a decimal point in the value?

In that case "\(0*(\d+\.\d+)\*"

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

No not always, this is also a example (005*A).
So if there is a decimal point and there is a other number before the decimal point then zero then capture and else strip al leading zero's
(000.96*A)  =  0.96
(001.96*A) = 1.96
(005*A) = 5

The amount of leading zero's can be different.

Edited by nend
Link to comment
Share on other sites

  • Solution

Why not use the Number() function for such a simple transformation?

ConsoleWrite(Number("000.96*A") & @CRLF)
ConsoleWrite(Number("001.96*A") & @CRLF)
ConsoleWrite(Number("005*A")    & @CRLF)

Outputs:

0.96
1.96
5

 

Edited by TheXman
Link to comment
Share on other sites

  • Developers

In case you are curious how to do that in regex: This is one should work:

#include <StringConstants.au3>
test("(000.96*A)")
test("(001.96*A)")
test("(012.96*A)")
test("(005*A)")
Func test($input)
    Local $array = StringRegExp($input, "\(0*((?:\d+\.\d+)?\d+)\*", $STR_REGEXPARRAYGLOBALMATCH, 1)
    ConsoleWrite($array[0] & @CRLF)
EndFunc   ;==>test

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...