Jump to content

StringRegExp again!


Recommended Posts

  • Moderators

Hi again,

Once more SRE has defeated me. >_< I can usually understand what is going on when I dissect one of the patterns, but trying to develop one from scratch is another kettle of fish altogether. :(

I am trying to get a section from a string, extracted from a path, which has the following format: aaaa\bbbb\cccc\ although it could be as short as just aaaa\. I want the section before the final \ - in the 2 cases above that means the bold portions cccc and aaaa. Obviously the sections could be of any length and could well contain spaces - normal folder names.

So far I have managed to do it if I remove the final \ first:

$sAnswer = StringRegExpReplace(StringTrimRight($sPathSegment, 1), "^.*\\", "")

but I cannot work out how to extract it with the final \ in place.

As this is in a loop, using StringTrimRight on each pass is costing me a lot of time - and yes I have tried recoding to reduce the need for the final \ but that was just causing me different problems elsewhere. :(

Anyone able to help?

Thanks in advance,

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 again,

Once more SRE has defeated me. >_< I can usually understand what is going on when I dissect one of the patterns, but trying to develop one from scratch is another kettle of fish altogether. :(

I am trying to get a section from a string, extracted from a path, which has the following format: aaaa\bbbb\cccc\ although it could be as short as just aaaa\. I want the section before the final \ - in the 2 cases above that means the bold portions cccc and aaaa. Obviously the sections could be of any length and could well contain spaces - normal folder names.

So far I have managed to do it if I remove the final \ first:

$sAnswer = StringRegExpReplace(StringTrimRight($sPathSegment, 1), "^.*\\", "")

but I cannot work out how to extract it with the final \ in place.

As this is in a loop, using StringTrimRight on each pass is costing me a lot of time - and yes I have tried recoding to reduce the need for the final \ but that was just causing me different problems elsewhere. :(

Anyone able to help?

Thanks in advance,

M23

Let me get this straight in my foggy mind.

aaa\bbb\ccc\ should give you ccc\

aaa\ should return aaa\

aaa\bbb\ should return bbb\

If I'm right so far then what is the StringTrimRight($sPathSegment, 1) doing in the RegExpReplace?

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Dude, but the heck are you using SRE for this simple hell thing?

Anyway, Your programming format is the same as me so I am helping you >_<

$sTest = 'aaa\bbb\ccc\'
$aSplit = StringSplit($sTest, '\')
$aAnswer = $aSplit[$aSplit[0] - 1]
ConsoleWrite($aAnswer)

OR your way

$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "")

WTH is wrong with the one line Autoit tag?

*edit: add his way of things.

Edited by athiwatc
Link to comment
Share on other sites

Dude, but the heck are you using SRE for this simple hell thing?

Anyway, Your programming format is the same as me so I am helping you >_<

$sTest = 'aaa\bbb\ccc\'
$aSplit = StringSplit($sTest, '\')
$aAnswer = $aSplit[$aSplit[0] - 1]
ConsoleWrite($aAnswer)

WTH is wrong with the one line Autoit tag?

Probably for a couple of reasons, the primary one being that he wants to learn SRE.

The code tags have an issue when there is just 1 line. Just add another line with ";" to solve the problem or click on popup to see the code.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Probably for a couple of reasons, the primary one being that he wants to learn SRE.

The code tags have an issue when there is just 1 line. Just add another line with ";" to solve the problem or click on popup to see the code.

I added the SRE for him, everyone is happy now!

Link to comment
Share on other sites

  • Moderators

GEOSoft,

Let me get this straight in my foggy mind.

aaa\bbb\ccc\ should give you ccc\

aaa\ should return aaa\

aaa\bbb\ should return bbb\

If I'm right so far then what is the StringTrimRight($sPathSegment, 1) doing in the RegExpReplace?

Your mind is far from foggy, but you are not quite there. The desired result is:

aaa\bbb\ccc\ should give you ccc

aaa\ should return aaa

aaa\bbb\ should return bbb

No final \

The StringTrimLeft is there because I found that if I removed the final \, I could develop an SRE pattern to extract the element of the string I wanted. I just cannot get a pattern to work with the final \ still there.

athiwatc,

I am using SRE because, as GEOSoft correctly guessed, I trying to get my brain around its use - albeit without a great deal of success at the moment. Furthermore, I am betting that SRE will be quicker than using _PathSplit - and as I am in a loop I am interested in gaining whatever time I can.

Thanks for your suggested SRE pattern, but I am afraid that it did not work:

$sPathSegment = "aaa\bbb\ccc\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "")
ConsoleWrite($sAnswer & @CRLF)

$sPathSegment = "aaa\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "")
ConsoleWrite($sAnswer & @CRLF)

$sPathSegment = "aaa\bbb\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "")
ConsoleWrite($sAnswer & @CRLF)

Result in SciTE console:
>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe "M:\Program\Au3 Scripts\fred.au3"    

aaa\

+>18:33:57 AutoIT3.exe ended.rc:0

Any other suggestions?

M23

Edit: Speeling again!

Edited by Melba23

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

GEOSoft,

Your mind is far from foggy, but you are not quite there. The desired result is:

aaa\bbb\ccc\ should give you ccc

aaa\ should return aaa

aaa\bbb\ should return bbb

No final \

The StringTrimLeft is there because I found that if I removed the final \, I could develop an SRE pattern to extract the element of the string I wanted. I just cannot get a pattern to work with the final \ still there.

athiwatc,

I am using SRE because, as GEOSoft correctly guessed, I trying to get my brain around its use - albeit without a great deal of success at the moment. Furthermore, I am betting that SRE will be quicker than using _PathSplit - and as I am in a loop I am interested in gaining whatever time I can.

Thanks for your suggested SRE pattern, but I am afraid that it did not work:

$sPathSegment = "aaa\bbb\ccc\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "")
ConsoleWrite($sAnswer & @CRLF)

$sPathSegment = "aaa\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "")
ConsoleWrite($sAnswer & @CRLF)

$sPathSegment = "aaa\bbb\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "")
ConsoleWrite($sAnswer & @CRLF)

Result in SciTE console:
>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe "M:\Program\Au3 Scripts\fred.au3"    

aaa\

+>18:33:57 AutoIT3.exe ended.rc:0

Any other suggestions?

M23

Edit: Speeling again!

Dude, again why are you using replace?

$sPathSegment = "aaa\bbb\"
$sAnswer = StringRegExp($sPathSegment, ".*\\(.*)\\",1)
ConsoleWrite($sAnswer[0] & @CRLF)

GoodLuck

*edit: add the goodluck

Edited by athiwatc
Link to comment
Share on other sites

Dude, again why are you using replace?

$sPathSegment = "aaa\bbb\"
$sAnswer = StringRegExp($sPathSegment, ".*\\(.*)\\",1)
ConsoleWrite($sAnswer[0] & @CRLF)

GoodLuck

*edit: add the goodluck

Because he wants to?? It's a good thing to learn.

@Melba23

If they always end in a backslash then

$sPathSegment = "aaaa\bbb\ccc\"
$sFldr = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "$1")
MsgBox(0, "Result", $sFldr)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Because he wants to?? It's a good thing to learn.

@Melba23

If they always end in a backslash then

$sPathSegment = "aaaa\bbb\ccc\"
$sFldr = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "$1")
MsgBox(0, "Result", $sFldr)

Okay I'm going to help him learn.

You should use this, in the future when you want to add stuff it won't get buggy. {}

$sPathSegment = "aaaa\bbb\ccc\"
$sFldr = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "${1}")
MsgBox(0, "Result", $sFldr)
Link to comment
Share on other sites

Okay I'm going to help him learn.

You should use this, in the future when you want to add stuff it won't get buggy. {}

$sPathSegment = "aaaa\bbb\ccc\"
$sFldr = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "${1}")
MsgBox(0, "Result", $sFldr)

for future reference the {} is not required when using $ to replace \. The "$1" is the same as "\1".

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

for future reference the {} is not required when using $ to replace \. The "$1" is the same as "\1".

I'm not telling that you are wrong I mean if in the future he want to do something like $13232 where 3232 is just numbers.

Link to comment
Share on other sites

I'm not telling that you are wrong I mean if in the future he want to do something like $13232 where 3232 is just numbers.

I understand now and you are correct. In that case it is required.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

GEOSoft,

Thank you for your suggestion - closest so far! I am sorry to say that it does not quite work if there is only the one section:

$sPathSegment = "aaa\bbb\ccc\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "$1")
ConsoleWrite($sAnswer & @CRLF)

$sPathSegment = "aaa\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "$1")
ConsoleWrite($sAnswer & @CRLF)

$sPathSegment = "aaa\bbb\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "$1")
ConsoleWrite($sAnswer & @CRLF)

SciTE console:

>Running:(3.3.1.1):C:\Program Files\AutoIt3\beta\autoit3.exe "M:\Program\Au3 Scripts\fred3.au3"    
ccc
aaa\
bbb
+>20:26:05 AutoIT3.exe ended.rc:0

Thanks for your time on this - I am learning a lot by trying to understand your patterns - and getting nearer to a solution all the time. >_<

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

GEOSoft,

Thank you for your suggestion - closest so far! I am sorry to say that it does not quite work if there is only the one section:

$sPathSegment = "aaa\bbb\ccc\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "$1")
ConsoleWrite($sAnswer & @CRLF)

$sPathSegment = "aaa\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "$1")
ConsoleWrite($sAnswer & @CRLF)

$sPathSegment = "aaa\bbb\"
$sAnswer = StringRegExpReplace($sPathSegment, ".*\\(.*)\\", "$1")
ConsoleWrite($sAnswer & @CRLF)

SciTE console:

>Running:(3.3.1.1):C:\Program Files\AutoIt3\beta\autoit3.exe "M:\Program\Au3 Scripts\fred3.au3"    
ccc
aaa\
bbb
+>20:26:05 AutoIT3.exe ended.rc:0

Thanks for your time on this - I am learning a lot by trying to understand your patterns - and getting nearer to a solution all the time. >_<

M23

For that we can solve the problem with this SRE

$sAnswer = StringRegExpReplace($sPathSegment,"(.*\\|^)(.*)\\", "$2")

At least it tested correctly for me. Also check your PMs

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

GEOSoft,

Thank you for that. I also see how you did it - something about SRE must be getting through. >_<

And, as I suspected, I gain a bit of time in my loop as well.

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

ConsoleWrite( _
    StringRegExpReplace("aaaa\bbbb\cccc\", "(.+?\\)*(.+?)(\\.*?(?!\\))", "$2") _
    & @CRLF)

That is actually the best expression to be using here and it meets all his requirements.

$sAnswer = StringRegExpReplace($sPathSegment, "(.+?\\)*(.+?)(\\.*?(?!\\))", "$2")

EDIT: The only reason I re-wrote the code posted by SmOke_N was to make it fit with the code you already have. My recomendation is to use the RegExp written by SmOke_N which is included in this post in a form that you were using.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

M23

Did you get that file I sent?

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

GEOSoft,

Yes, thank you. And thanks for the encouragement - I feel I need it!

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

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