Jump to content
Sign in to follow this  
BatMan22

Regex101 different matching then Autoit?

Recommended Posts

BatMan22

Regex101 gives me a different matching then Autoit, and I don't know what I'm doing wrong, guessing regex versions are slightly different.. help?

What I am running: 

$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
$preedit = StringRegExpReplace($preedit, ".+?(?=\d)", "") ; Delete Everything before numbers

What I am expecting is for it to match everything BEFORE 0.0118, which works on regex101 and to delete it.. What I end up with is "3".

Share this post


Link to post
Share on other sites
golfinhu
1 hour ago, BatMan22 said:

Regex101 gives me a different matching then Autoit, and I don't know what I'm doing wrong, guessing regex versions are slightly different.. help?

What I am running: 

$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
$preedit = StringRegExpReplace($preedit, ".+?(?=\d)", "") ; Delete Everything before numbers

What I am expecting is for it to match everything BEFORE 0.0118, which works on regex101 and to delete it.. What I end up with is "3".

$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
$preedit = StringRegExpReplace($preedit, "^(.+?\d+\.\d+)", "") ; Delete Everything before numbers

MsgBox(0,'',$preedit)

try it.

Share this post


Link to post
Share on other sites
BatMan22
Posted (edited)

@golfinhu, am I missing something? I ran it, and it matched the 0.0118 as well, I want to keep that. I just want to delete everything before the first number, 0.0118, but I need it to be smart because that number could be anything. 

Edited by BatMan22

Share this post


Link to post
Share on other sites
iamtheky
Posted (edited)
1 hour ago, BatMan22 said:

Regex101 gives me a different matching then Autoit, and I don't know what I'm doing wrong, guessing regex versions are slightly different.. help?

 

Its fine, you just only want to replace the first match with the blank string

$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
msgbox(0, '' , StringRegExpReplace($preedit, ".+?(?=\d)", "" , 1))

 

You are really matching everything in groups of things up to the next number with that regex, so whether it is the best one for the job is debatable

#include<array.au3>
$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
_ArrayDisplay(StringRegExp($preedit, ".+?(?=\d)",  3))

 

Edited by iamtheky
  • Like 1

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
TheXman

If you want to delete everything from the beginning of the line until the first number, then this should work also:

$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
$preedit = StringRegExpReplace($preedit, "^[^\d]*", "")
ConsoleWrite(StringFormat("$preedit = %s", $preedit) & @CRLF)

 

  • Like 1

Whoever said that there's no such thing as a dumb question, probably asked a lot of DUMB questions.  Dumb questions are ones that you can easily find the answer to on your own.

Share this post


Link to post
Share on other sites
TheXman
Posted (edited)

 

11 hours ago, BatMan22 said:

Regex101 gives me a different matching then Autoit, and I don't know what I'm doing wrong, guessing regex versions are slightly different.. help?

What I am running: 

$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
$preedit = StringRegExpReplace($preedit, ".+?(?=\d)", "") ; Delete Everything before numbers

 

 

The reason that you didn't get the expected result is because, by default, the StringRegExReplace() does a global replace.  If you would have added a "1" for the count parameter, it would have worked as you expected.

$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
$preedit = StringRegExpReplace($preedit, ".+?(?=\d)", "", 1) ; Delete Everything before numbers
ConsoleWrite(StringFormat("$preedit = %s", $preedit) & @CRLF)

Or you could have simply asserted that it should only look from the start.  Then you wouldn't have needed to add a count parameter.  It would have looked like this:

$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
$preedit = StringRegExpReplace($preedit, "^.+?(?=\d)", "") ; Delete Everything before numbers
ConsoleWrite(StringFormat("$preedit = %s", $preedit) & @CRLF)

 

Edited by TheXman
Added an additional way that original code could have been modified to work.
  • Like 1

Whoever said that there's no such thing as a dumb question, probably asked a lot of DUMB questions.  Dumb questions are ones that you can easily find the answer to on your own.

Share this post


Link to post
Share on other sites
BatMan22
18 hours ago, TheXman said:

The reason that you didn't get the expected result is because, by default, the StringRegExReplace() does a global replace.  If you would have added a "1" for the count parameter, it would have worked as you expected.

$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
$preedit = StringRegExpReplace($preedit, ".+?(?=\d)", "", 1) ; Delete Everything before numbers
ConsoleWrite(StringFormat("$preedit = %s", $preedit) & @CRLF)

Or you could have simply asserted that it should only look from the start.  Then you wouldn't have needed to add a count parameter.  It would have looked like this:

$preedit = "Cl  K            0.0118 Wt %          7.91E-4    121.3         290.3 "
$preedit = StringRegExpReplace($preedit, "^.+?(?=\d)", "") ; Delete Everything before numbers
ConsoleWrite(StringFormat("$preedit = %s", $preedit) & @CRLF)

THIS! This is what I was looking for.. Thank you dude.

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
Sign in to follow this  

×