Sfikes

Regular Expression PLEASE HELP!

11 posts in this topic

#1 ·  Posted

$time = "23:20:23"

$hey = StringRegExp($time, '(\w:\w:\w)')

If $hey == 1 Then
   Send("Yes")
EndIf

If $hey == 0 Then
   Send("No")
EndIf

 

So i wanna know if my string has any numbers. To do that i used StringRegExp in order to check if the string was containing any numbers.

So simply i want it to read like this: Any:Any:Any including the ":" but it always returns 0 as a "no match" which must mean that this script doesnt work. Any suggestions?

Share this post


Link to post
Share on other sites



#2 ·  Posted

Sfikes,

Try this:

#include <MsgBoxConstants.au3>

$time = "23:20:23"

$hey = StringRegExp($time, '(\d+:\d+:\d+)')

If $hey = 1 Then
   MsgBox($MB_SYSTEMMODAL, "Result", "Yes")
   Else
   MsgBox($MB_SYSTEMMODAL, "Result", "No")
EndIf

The difference is that I am looking for at least one digit in section - you were looking for only a single digit, letter or underscore.

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

#3 ·  Posted

2 minutes ago, Melba23 said:
$hey = StringRegExp($time, '(\d+:\d+:\d+)')

 

Thanks alot man! Works like a charm! Could you explain for me what  \d+: means? I really wanna learn it.

Share this post


Link to post
Share on other sites

#4 ·  Posted

Sfikes,

Reading the Help file page for StringRegExp will explain most of the possible parameters. In this case I used:

Quote

\d  -  Matches any decimal digit

in conjunction with:

Quote
+   -  1 or more

So it will match if we have 3 groups of one or more digits separated by colons.

To be really precise, we should really ask for groups of one or two digits which comes out as:

$hey = StringRegExp($time, '(\d{1,2}:\d{1,2}:\d{1,2})')

Try it with various strings and see how it performs.

RegExes are quite tricky to learn (my brain still bleeds when developing a complex pattern) but are well worth the effort - I always recommend this site as a good place to start.

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

#5 ·  Posted

You are awesome thanks alot!

Share this post


Link to post
Share on other sites

#6 ·  Posted

31 minutes ago, Melba23 said:

Sfikes,

Try this:

#include <MsgBoxConstants.au3>

$time = "23:20:23"

$hey = StringRegExp($time, '(\d+:\d+:\d+)')

If $hey = 1 Then
   MsgBox($MB_SYSTEMMODAL, "Result", "Yes")
   Else
   MsgBox($MB_SYSTEMMODAL, "Result", "No")
EndIf

The difference is that I am looking for at least one digit in section - you were looking for only a single digit, letter or underscore.

M23

I never understood (or invested not enough time) StringRegExp. I just tested your eample with a fantasy time:

$time = "239:204:23"

and i got also yes.

Share this post


Link to post
Share on other sites

#7 ·  Posted

Sfikes,

Delighted I could help.

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

#8 ·  Posted

In the future, these sites may help you with RegEx as they certainly helped me

http://regexr.com/ - A playground for making RegEx

https://regex101.com/-  Another playground for making RegEx

https://txt2re.com/ - Visually see and extract the data you want

http://regex.inginf.units.it/ - Builds a RegEx based on examples you provide

https://regexone.com/ - Learn RegEx

Note: These sites are not made for specifically for AutoIt coding, but it's much easier than hitting F5 a dozen times to get what you want.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

AutoBert,

Look at the other pattern I posted - that is more precise and only accepts one or two digits per group.

M23

Edited by Melba23
Typo

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

#10 ·  Posted

Just a note about the good links provided above: favor regex101.com as it offers the same regexp flavor as Autoit (PCRE). The reference documentation for PCRE implemented in AutoIt is http://www.pcre.org/original/doc/html/ (AutoIt doesn't implement PCRE2, yet).


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#11 ·  Posted

1 hour ago, Sfikes said:

So i wanna know if my string has any numbers.

$time = "23:20:23"
;~ $time = "ElevenTwenty"

msgbox(0, '' , AscW(stringleft($time , 1)) > 47 AND AscW(stringleft($time , 1)) < 58  ? "Yup" : "Nope")

 


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

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