Newb Posted March 23, 2012 Share Posted March 23, 2012 I studied regexp this afternoon and got a good grasp on it.But in autoit a lot is being handled different and I was like so, i was trying to get years out of a string and didn't managed to do it, even after i studied them and managed to make them work in other languages....so, this is the code#include <Array.au3> $ok="1945 is the year 1945 is a good year 1954 is not the year because it's too high but 1945 is" $Result=StringRegExp($ok,"(1[1-9][1-5][^4-9])",0) MsgBox(0,"kk",$Result) _ArrayDisplay($Result,"Results are:")Basically it's only an exercise and I want to get any date lower than 1954.Also, a question on the help file example$array = StringRegExp('<test>a</test> <test>b</test> <test>c</Test>', '<(?i)test>(.*?)</(?i)test>', 1, $nOffset)this was the code. In the tutorial (not pcrepattern, which was horrible)this is what the tutorial saidThen what it means (.*?)... it should check if any charachter ( . ) may not appear or appear much times (*) or it may appear once or not appear at all (?).Can they e mixed or this means something else?Thanks I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it. Link to comment Share on other sites More sharing options...
iamtheky Posted March 23, 2012 Share Posted March 23, 2012 (edited) use regexp to get all date strings, then evaluate...im sure cooler examples from more skilled folks will be on the way. #include<array.au3> $ok="1945 is the year 1943 is a good year 1954 is not the year because it's too high but 1935 is" $okArray = stringregexp($ok , "dddd" , 3) _arraydisplay($okArray , "BEFORE") for $i =ubound ($okArray) - 1 to 0 step -1 if number($okArray[$i]) > 1953 Then _arrayDelete($okArray , $i) Next _arraydisplay($okArray , "AFTER") Edited March 23, 2012 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 23, 2012 Moderators Share Posted March 23, 2012 Newb, I studied regexp this afternoon and got a good grasp on itCongratulations - most people spend a lot longer looking at SREs and still have problems. A RegEx guru will no doubt be along in a moment with a better solution but this pattern does what you want: #include <Array.au3> $ok="1945 is the year 1945 is a good year 1954 is not the year because it's too high but 1945 is" $Result=StringRegExp($ok,"(1[1-9][0-4][0-9]|1[1-9]5[0-3])", 3) MsgBox(0,"kk",$Result) _ArrayDisplay($Result,"Results are:") M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
AdamUL Posted March 23, 2012 Share Posted March 23, 2012 (edited) Then what it means (.*?)... it should check if any charachter ( . ) may not appear or appear much times (*) or it may appear once or not appear at all (?).Look at the help file again for the other explanation of ? after a repeating character, which are "{x}, {x,}, {0,x}, {x,y}, *, + , or ?".? (after a repeating character): Find the smallest match instead of the largest.* is a repeating character. So .*? mean match the smallest match for any character. For the meaning in your quote, you want (.*)? for a capturing group or (?:.*)? for a non-capturing group.Adam Edited March 23, 2012 by AdamUL Link to comment Share on other sites More sharing options...
Newb Posted March 23, 2012 Author Share Posted March 23, 2012 (edited) use regexp to get all date strings, then evaluate...im sure cooler examples from more skilled folks will be on the way. #include<array.au3> $ok="1945 is the year 1943 is a good year 1954 is not the year because it's too high but 1935 is" $okArray = stringregexp($ok , "dddd" , 3) _arraydisplay($okArray , "BEFORE") for $i =ubound ($okArray) - 1 to 0 step -1 if number($okArray[$i]) > 1953 Then _arrayDelete($okArray , $i) Next _arraydisplay($okArray , "AFTER") Okay, I'll try that too, just to learn new methods Newb, Congratulations - most people spend a lot longer looking at SREs and still have problems. A RegEx guru will no doubt be along in a moment with a better solution but this pattern does what you want: #include <Array.au3> $ok="1945 is the year 1945 is a good year 1954 is not the year because it's too high but 1945 is" $Result=StringRegExp($ok,"(1[1-9][0-4][0-9]|1[1-9]5[0-3])", 3) MsgBox(0,"kk",$Result) _ArrayDisplay($Result,"Results are:") M23 I don't know why, but I found the so easy I couldn't believe it... i learnt them in like an hour and half or so without never having any previuos experience with that (ye i asked in the forums here but i never really read anything or made exercises with them before this afternoon).... I've also figured out the stupid mistake I did (1[1-9][1-5][^4-9]) was wrong because of [1-5] since dates have 0s and [^4-9] should have been [^6-9]. Yes, it can be improved for better recognition, because it would still take 1955 in this case, if taken alone. So i guess your second example after the or | would be good if put like this 1[0-9][0-5][0-3] and it would take all dates before 1954 back to 1000. On the counter party I'm learning C# for some huge projects and I'm having an hard time on it.... so many things... meh! If you have a good tutorial on that I would like that And, Ok, the previous regex returns an array with all references to the results. But how can I choose without the array which one to choose? For example I want my regex to pick the third Autoit found in this string (the one in red) "Autoit is a good language Autoit works fine Autoit has a good community" I know there is an argument which let you choose from which charachter start to search, but I wanted to know if there is a way to choose it directly from the regexp, without having to pass arguments to functions. Look at the help file again for the other explanation of ? after a repeating character, which are "{x}, {x,}, {0,x}, {x,y}, *, + , or ?". * is a repeating character. So .*? mean match the smallest match for any character. For the meaning in your quote, you want (.*)? for a capturing group or (?:.*)? for a non-capturing group. Adam So, if (t*?) applied to the string "autoitttttttt" It would take one t? Thanks all Edited March 23, 2012 by Newb I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 23, 2012 Moderators Share Posted March 23, 2012 Newb, 1[0-9][0-5][0-3] [...] would take all dates before 1954 back to 1000No it will not - as you will exclude all dates ending in 4 to 9. You need to allow those final digits in all decades earlier than 1950 - which is why I used the "|" operator to have 2 options. I want my regex to pick the third Autoit found in this stringAn SRE is not the tool to do this. StringInStr will do it for you much more easily and will even tell you the character index as well - which if you want to change the value is very useful. SREs are useful when you need to match several things which are not identical but have a similar pattern - like the dates you used in your first question. If all instances are identical as in this case, there is no point in using SREs. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Newb Posted March 23, 2012 Author Share Posted March 23, 2012 Ok, true. Thanks. I used sres to find the third result just because there might not be such advanced functions as StringInStr in other languages as C# or whatever and i might want to use a regexp to check what i want (Yeah, i might find libraries that do that or write my own functions or maybe C# has some ready-to-use functions for that like autoit, but would be cool to be able to do it with regex) Thanks again. I might have other questions in future I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 23, 2012 Moderators Share Posted March 23, 2012 Newb, This is an example of how you might use a RegEx to search for and locate several instances in a string: #include <Array.au3> Global $aArray[3] $sString = "Bla bla bla" $iOffset = 1 For $i = 0 To 2 $aTemp = StringRegExp($sString, "a", 1, $iOffset) $iOffset = @extended $aArray[$i] = $iOffset - 1 Next _ArrayDisplay($aArray) I hope it helps. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Newb Posted March 24, 2012 Author Share Posted March 24, 2012 (edited) Ok i'll try the code and make you know Melba23, many thanks for the help! You're always helpful and kind. Anyway i came up with the best solution (i guess) for the years before 1954. It should be (1[0-9]([0-4][0-9]|[0-5][0-3]))|1[0-8][0-9][0-9] I edited this post like 30 times lol, but finally i got it basically it catches 1900-1953 or 1000-899. Works on a Perl Expression Tester (I used http://www.perlfect.com/articles/regextutor.shtml) and works fine with autoit too. My other question is: Why it shows only the full thing it catched (all the numbers this regexp can catch) with the 3 flag as argument? ($Result=StringRegExp($ok,"1[0-9][0-4][0-9]|195[0-3]",3)) The flag 2 shows only the first 2 1945 and the flag 4 shows 4 empty spaces on the string "1945 is the year 1945 is a good year 1953 is not the year because it's too high but 1945 is" looked at help file but didn't understand much about those flags- @Melba23 Looked at your code today: still don't get what @extended is (or to say it better, i know what it is because i saw it when array displayed but wanted more infos about it and how can i use it in other ways) helpfile says Flag = 1 or 2 : @Error Meaning 0 Array is valid. Check @Extended for next offset So what it should be, the next charachter after a found string or what??? And what's the use of it? How can I use it for (in all his extents)? Edited March 24, 2012 by Newb I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now