Jump to content

Regex101 different matching then Autoit?


Recommended Posts

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".

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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
Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

 

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.
Link to comment
Share on other sites

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.

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...