Jump to content
Sign in to follow this  
Newb

Regexp , help

Recommended Posts

Newb

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 :oops:

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 said

Posted Image

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 (?).

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.

Share this post


Link to post
Share on other sites
iamtheky

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 by boththose

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

Share this post


Link to post
Share on other sites
Melba23

Newb,

I studied regexp this afternoon and got a good grasp on it

Congratulations - most people spend a lot longer looking at SREs and still have problems. :oops:

A RegEx guru will no doubt be along in a moment with a better solution but this pattern does what you want: :bye:

#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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
AdamUL

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 by AdamUL

Share this post


Link to post
Share on other sites
Newb

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

A RegEx guru will no doubt be along in a moment with a better solution but this pattern does what you want: :doh:

#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 :bye:

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

Share this post


Link to post
Share on other sites
Melba23

Newb,

1[0-9][0-5][0-3] [...] would take all dates before 1954 back to 1000

No 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. :oops:

I want my regex to pick the third Autoit found in this string

An 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. :bye:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Newb

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.

Share this post


Link to post
Share on other sites
Melba23

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

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Newb

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

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  

×