Jump to content

([SOLVED] regexp question)? - idiot ocurred 0 or 1 times


kylomas
 Share

Recommended Posts

Good Morning,

Given the following

1 12 123 9 98 987 11
*- there is a space at the beginning and end of the string

(\d{1,2})
and
(\d\d?)
return the same result. Are these stmts functionally equivalent or is this a coincidence the pattern I am using?

If they are functionally equivalent, when is one preferred over another?

Also,

The first "12" is not reported a a match. Is this because the space before it is "consumed" by the previous match?

Thanks,

kylomas

Note - I am reading through the tutorials cited by various members, including jchd, however, there are as many unanswered questions as answered questions!

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Moderators

kylomas,

Disclaimer: My opinion only and I am far from expert in SREs! :D

Are these stmts functionally equivalent

Yes.

Is this because the space before it is "consumed" by the previous match?

It might be. What are you using as a pattern and what exactly are you trying to pull from this string? All the 1/2 digit numbers? All 1/2 digit combinations preceded by a space? All 1/2 digit combinations preceded and followed by a space? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

Hi,

yes these are equal.

"(d{1,2})"

d - matches any didgit
{1,2} - tells the engine to match the previous part ( d ) one or two times.

"(dd?)"

d - matches any didgit
d - matches any didgit
? - tells the engine to match the previous part ( the second d ) zero or one times.

So they both allow for one or two digits, and there are a whole lot of other ways to reach the same outcome.

Edit: You might also want to check out the example I gave here:

Edited by Robjong
Link to comment
Share on other sites

Isn't the first one more optimal?

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Not really, they should result in the same logic in the engine, but to read the expression one might prefer the former.

The matches can be one or two digits. Once the engine encounters a digit the backtrack position is stored.

The engine then goes on to see if it can match a second digit right after the first match, stores the result if it is and continues from there.

If not the engine backtracks to the last known position and saves the result and continues from there.

Edit: added explanation.

Edited by Robjong
Link to comment
Share on other sites

Both will return values that you probably do not expect...such as when you have 3 digits, it will be broken up into a 2digit integer and a 1digit integer.

You need to consider string begin and string ends, or include all following digits: (d+)

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Both will return values that you probably do not expect...such as when you have 3 digits, it will be broken up into a 2digit integer and a 1digit integer.

You need to consider string begin and string ends, or include all following digits: (d+)

Excatly, but this might not be relevant to the OP. Btw. This is excatly what the example I linked to shows.
Link to comment
Share on other sites

  • Moderators

jdelaney,

You need to consider string begin and string ends

I quite agree with you, which is why I asked the OP what he actually expects as a return. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

whoops, misquoted...boundaries are key:

#include <Array.au3>
$a = StringRegExp ( "1 12 123 9 98 987 11", "b(d{1,2})b", 3 )
_ArrayDisplay ( $a )
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

@all - Thanks for the responses.

My first question is answered.

My second question cannot be answered because I fucked up. The pattern should have read

( d{1,2} )
. The pattern, entered correctly, is returning "12" and "9". Is that because of this from Robjong

The matches can be one or two digits. Once the engine encounters a digit the backtrack position is stored.

The engine then goes on to see if it can match a second digit right after the first match, stores the result if it is and continues from there.

If not the engine backtracks to the last known position and saves the result and continues from there.

???????

Again, thanks for the prompt replies and apologies for the screw up...

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

jdelaney,

I am trying to get my arms around this cryptic language. I believe that Robjong answered this question

The first "12" is not reported a a match. Is this because the space before it is "consumed" by the previous match?

or, am I missing something again!

Apologies, again,for posting the regexp incorrectly, and, thank you for your time!

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Moderators

kylomas,

Please post what you want as a return from the RegEx as both jdelaney and I asked above - until then we cannot really answer your question! :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

M23,

Apologies if I am not being clear enough. My questions were about syntax and behavior of regexp and have been answered.

Thank you all for your time.

kylomas

P.S. If I seem to be generally confused, it is because I am! I blame too many regexp tutorials in the last 48 hours for this!

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Moderators

kylomas,

If you are happy that your questions have been answered, then fine.... :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

I have a regexp search that works everywhere but in autoit3 it goes like this:

$sd = "Sent by Team:SYSADMIN User:jm5751"

$r = StringRegExp($sd, '([a-z]{2}d{3})(d|[a-z])',01)

ConsoleWrite('user id: ' & $r[0] & @crlf)

I can get the jm575 but I can't get the 1 after or the alpha if there is one. What can I do to fix this?

Thanks

Link to comment
Share on other sites

If the pattern in the second capture group matches text in the test string, that text will be in the second element of the returned array.

$sd = "Sent by Team:SYSADMIN User:jm5751"
$r = StringRegExp($sd, '([a-z]{2}d{3})([da-z]?)', 1)
ConsoleWrite('user id: ' & $r[0] & $r[1] & @CRLF)

@jmosley

You should have started another thread (your own thread) instead of appending to this thread.

Link to comment
Share on other sites

....

@jmosley

You should have started another thread (your own thread) instead of appending to this thread.

..... How do I start a new thread?

When you are logged on to these forums, there is a button made visible near the top of each forum that says "Start New Topic". This will allow you to weave your own topic within the fabric of that chosen forum.
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...