AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

RegExMatch isn't working anymore.

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
StreetRider



Joined: 19 Mar 2008
Posts: 85
Location: Euclid, Ohio

PostPosted: Thu May 15, 2008 8:02 pm    Post subject: RegExMatch isn't working anymore. Reply with quote

I hd gotten a great answer from one of the Power Users here and it seemed to work, now it doesn't.

It should return the X5.234

Code:

PullBack= G01X5.234Y7.2343
RegExMatch(PullBack, "\bx.+?(?=\s|$)", PullBackLine)
Msg,  %PullBackLine%




There may or may not be a space after the G01 and there might not be a space or a Y at the end of the var.

PullBack= G01X1.2345Y1.2345
PullBack= G01 X1.2345Y1.2345
Pullback= G01X1.2345
PullBack= G01 X1.2345
PullBack= G01 G01 X1.2345

The length of the string could be more or less.

PullBack= G01 X1.1
PullBack= x1.23232323232Y1.1

Can anyone help out this RexEx idot?

Thank you in advance
_________________
StreetRider
Euclid. Ohio

"New and Loving it!"
Back to top
View user's profile Send private message
Icarus



Joined: 24 Nov 2005
Posts: 440

PostPosted: Thu May 15, 2008 9:02 pm    Post subject: Reply with quote

I dont know what was the reason for the regex that you used.
The \b in front of the X means word boundary - so I do not know how it worked in cases where there was anything but space in front og the X.

This below example pulls out the X value from all of your examples:
Code:
;PullBack= G01X1.2345Y1.2345
;PullBack= G01 X1.2345Y1.2345
;Pullback= G01X1.2345
;PullBack= G01 X1.2345
;PullBack= G01 G01 X1.2345
;PullBack= G01 X1.1
;PullBack= x1.23232323232Y1.1

PullBack= G01X5.234Y7.2343

RegExMatch(PullBack, "([Xx]\d+\.\d+)", PullBackLine)
Msgbox  %PullBackLine1%




EDIT: If you want the number only, use this regex instead:
RegExMatch(PullBack, "[Xx](\d+\.\d+)", PullBackLine)
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
StreetRider



Joined: 19 Mar 2008
Posts: 85
Location: Euclid, Ohio

PostPosted: Fri May 16, 2008 11:56 am    Post subject: Thank You. Reply with quote

Thank you for your answer. I will try that today. As well as my next project is to teach myself those damn regex codes.

I was given that line as a answer to a call for help. I thikn I did nopt explain myself well enough to to original person.

Thank you again for your time.
_________________
StreetRider
Euclid. Ohio

"New and Loving it!"
Back to top
View user's profile Send private message
StreetRider



Joined: 19 Mar 2008
Posts: 85
Location: Euclid, Ohio

PostPosted: Mon May 19, 2008 1:05 pm    Post subject: Works in test script only. Reply with quote

When I use this code in my program it returns blanks. In test scripts it seems to work fine.

the problem seems to be that sometimes LastLine will have a negative number and sometimes a positive.

LastLine=G1-X1.234
LastLine=G1x1.234

Code:

LastLine= G1-x1.234
StlLine= y1.2345

MsgBox, LastLine %LastLine%`nSecond to Last Line %StlLine%
IfInString, LastLine, x
   PullBack:= LastLine ;Pullback is located in Last Line
ELSE
   PullBack:= StlLine ;PullBack is located in Second To Last Line
;
RegExMatch(PullBack, "([Xx]\d+\.\d+)", PullBackNumber)
RegExMatch(PullBack, "[Xx](\d+\.\d+)", PullBackLine)
;
MsgBox, PullBack %PullBack%`nPullBackLine %PullBackLine%`nPullBackNumber %PullBackNumber%


_________________
StreetRider
Euclid. Ohio

"New and Loving it!"
Back to top
View user's profile Send private message
Icarus



Joined: 24 Nov 2005
Posts: 440

PostPosted: Mon May 19, 2008 1:35 pm    Post subject: Reply with quote

I dont understand - do you want the minus to be included in the capture?
If so , use this
RegExMatch(PullBack, "(-?[Xx]\d+\.\d+)", PullBackNumber)
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
StreetRider



Joined: 19 Mar 2008
Posts: 85
Location: Euclid, Ohio

PostPosted: Mon May 19, 2008 4:17 pm    Post subject: Thank you. Reply with quote

Here is the problem.

I need to use the number else where as both a positive and a negative.

It seems that if the "Haystack" has the "-" then it returned no string. If it was a postitive number it returns just fine.

If it returns a whole number I can add the "-".

Basically I need to extract jut the number not the "-" as well.

It seems like the other RegEx line I was given does not like the "-".
_________________
StreetRider
Euclid. Ohio

"New and Loving it!"
Back to top
View user's profile Send private message
Icarus



Joined: 24 Nov 2005
Posts: 440

PostPosted: Mon May 19, 2008 5:18 pm    Post subject: Reply with quote

I do not know what is the other regex you are referring to. If you talk about the regex I provided, then it seems to work just fine, with or without a minus in the string.

The below code demonstrates how both the sample strings that you have provided are captured:
Code:

RegExMatch( "G1-x1.234", "[Xx](\d+\.\d+)", A )
RegExMatch( "G1x1.234" , "[Xx](\d+\.\d+)", B )
Msgbox %A1%`n%B1%


Now, if you mean there can also be a minus sign between the "x" and the number, use this regex instead:
Code:

RegExMatch( "G1x-1.234", "[Xx]-?(\d+\.\d+)", A )
RegExMatch( "G1-x1.234", "[Xx]-?(\d+\.\d+)", B )
RegExMatch( "G1x1.234" , "[Xx]-?(\d+\.\d+)", C )
Msgbox A = %A1%`nB = %B1%`nC = %C1%


Just to be clear. The second regex looks for the following:
Any string that contains an "X" followed by an optional "-", and followed by a number-dot-number is captured, the number-dot-number is stored in the capture variable, item #1 (e.g. A1)

It doesnt matter what comes before the X or what comes after the number.

If you mean something else, please tell me what are the various input strings that are possible, and an example of what do you want to extract.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
StreetRider



Joined: 19 Mar 2008
Posts: 85
Location: Euclid, Ohio

PostPosted: Mon May 19, 2008 5:33 pm    Post subject: By George I think you've got it. Reply with quote

Thank you for all of your help.


I think your last attempt did it.

possible varible strings are as follows.

G01X-1.1234
G01X-1.234Y1.123
G01X1.1234
G01X1.1234Y1.1234
X1.234
X-1.1234
X10.1234

These are possible eninding points for a CAD-CAM created CNC file. I need to parse the file replacing G and M-Codes. This I have worked out.But, at the end of the program I need to find the Last X move and re-insert it into the program in two other places.

All the other parts I have figured out, and it works nicely. And faster than I had hoped. Once I have this little problem worked out I can complete this and move onto bigger and better things....

Like learning to understand RegEx... <g>

Thank you again.


Streetrider
_________________
StreetRider
Euclid. Ohio

"New and Loving it!"
Back to top
View user's profile Send private message
Icarus



Joined: 24 Nov 2005
Posts: 440

PostPosted: Mon May 19, 2008 6:14 pm    Post subject: Reply with quote

Yes, judging by your examples, you need the second regex.
Code:

S1 = G01X-1.1234
S2 = G01X-1.234Y1.123
S3 = G01X1.1234
S4 = G01X1.1234Y1.1234
S5 = X1.234
S6 = X-1.1234
S7 = X10.1234

TheRegEx := "[Xx]-?(\d+\.\d+)"
AllResults := ""

Loop 7 {
   RegExMatch( S%A_Index% , TheRegEx, Result )
   AllResults .= "Extracted`t" . Result1 . "`t`tfrom " . S%A_Index% . "`n"
}

Msgbox 32,,%AllResults%


This code shows all the results.

The regular expression is simple: [Xx]-?(\d+\.\d+)
[Xx] = a group (inside []) of either lowercase or uppercase X
-? = an optional minus sign. the question mark means "0 or 1 appearances"
() = anything inside () will be captured (in our case, in the "Result" array)
\d+ = one or more digits (\d = digit, + = one or more)
\. = a dot

The AHK Regular Expression Reference (in the help) is quite useful and short. I am sure you can get the hang of it.

Glad I could help and good luck with your CNC Smile

EDIT:
If you care about the sign, use the following loop as an example.
In this case, we capture two things - the number and the minus (if it exists) in two separate variables.
Code:

TheRegEx := "[Xx](-?)(\d+\.\d+)"
AllResults := ""

Loop 7 {
   RegExMatch( S%A_Index% , TheRegEx, Result )
   AllResults .= "Extracted`t" . Result2 . "`t`tfrom " . S%A_Index% . "`n"
   If( Result1 = "-" )
      AllResults .= "   Previous value was negative`n"
}

Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
StreetRider



Joined: 19 Mar 2008
Posts: 85
Location: Euclid, Ohio

PostPosted: Mon May 19, 2008 7:26 pm    Post subject: Thank YOU. Reply with quote

thak you for explaining th RexEx line. That is the first time I have seen someone take the time, that makes sense.

I could use a IFInString to send the variable to the right RegEx line. or a If contains....


If var contains -
RegEx
ELSE
RegEx
_________________
StreetRider
Euclid. Ohio

"New and Loving it!"
Back to top
View user's profile Send private message
Icarus



Joined: 24 Nov 2005
Posts: 440

PostPosted: Mon May 19, 2008 7:45 pm    Post subject: Reply with quote

No need for any IFs. The regex I used is good for both negative and positive values.
Furthermore, if you want the sign to be a part of the captured number, just use this regex:
TheRegEx := "[Xx](-?\d+\.\d+)"

All of your examples will work with this regex, and the captured result ("Result1") will contain the number, and its sign.

EDIT:
Then, if you want to do different things if the value is negative or positive, just ask "If( Result1 > 0 )"
From this point on, treat it as a number.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
StreetRider



Joined: 19 Mar 2008
Posts: 85
Location: Euclid, Ohio

PostPosted: Mon May 19, 2008 8:11 pm    Post subject: Thank you Reply with quote

Thakn you for taking the time for this. I need to learn RegEx. For this type of thing it is so much quicker.
_________________
StreetRider
Euclid. Ohio

"New and Loving it!"
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group