Jump to content

regex question [SOLVED]


nend
 Share

Recommended Posts

Hi,

I'm trying to match and replace only if the & is not between white spaces.

kk&kk or kk& kk or kk &kk wil be kk & kk so the & is always surounded by whitespaces.

 

$string = "kk&kk"

ConsoleWrite(StringRegExpReplace($string, "([^\s]&[^\s])", " & ") & @CRLF)

kk they are just some character, it can be any character or any word.

Edited by nend
Link to comment
Share on other sites

Is this what you mean?

ConsoleWrite(doThingy("kk&kk") & @CRLF)
ConsoleWrite(doThingy("kk& kk") & @CRLF)
ConsoleWrite(doThingy("kk &kk") & @CRLF)
ConsoleWrite(doThingy("kk & kk") & @CRLF)
ConsoleWrite(doThingy("kk     &       kk") & @CRLF)
ConsoleWrite(doThingy("   & foo &bar &foobar& bar& foo") & @CRLF)

Func doThingy($s)
    Return StringRegExpReplace($s, "\s*&\s*", " & ")
EndFunc   ;==>doThingy

Output:

kk & kk
kk & kk
kk & kk
kk & kk
kk & kk
 & foo & bar & foobar & bar & foo

 

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Trying to understand how it works.

StringRegExpReplace($s, "\s*&\s*", " & ")

So replace "\s*&\s*" meaning /s Match any whitespace character, * anything after it

& AND the same thing again? ...with the string provided.

Does it mean that the * comprises lots of characters or just one?

Did i get it right? This regexp is still confusing to me. How to construct the pattern.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

The asterisk in regex is unlike the asterisk in for example a regular filespec, with which you are probably confusing it. In a filespec it just means "zero or more characters". (auto*.bat matches auto.bat, autoexec.bat auto_hello.bat etc.)

Instead, in regex, it refers to the thing (the "token") that precedes it, and matches "zero or more repetitions of the preceding token". That token can be a character set (like in this case the set in character class \s, or something like [:alnum:] ), but it could also be a character set like [a-g] or a capturing group like (red|green|blue) (the pipe means OR).

Note: the + means almost the same thing, but then "one or more repetitions of the preceding token".

Note: there is another gotcha (well, many others, regex can be tricky sometimes :) ) in the difference between filespecs and regex patterns: the period (.) . In regex it means "any character".

So putting all that together, if you take auto*.bat as your regex pattern, it would mean:

Literal "aut", followed by zero or more occurrences of literal "o", followed by any character, followed by literal "bat".

So it would match things you may not expect it to match thinking from a used-to-filespecs perspective, like:

  • autoXbat
  • autoooooooooooooooXbat
  • autXbat (<-- note: zero occurrences of the "o" !)
  • autobat (<-- read: zero occurrences of the "o", followed by one character before "bat" which matches the dot, and happens to be a literal "o")

... but "autbat" would not match, as there is no character in there to match the period (which requires one character) between "aut" and "bat".

Note that regular expressions have nothing to do with files or filespecs.

Last note: to specify what you originally thought * did, namely "zero or more characters", in regex you would use ".*", which would mean: zero or more repetitions of <any character>. So: abc.*xyz would be matched by "abcxyz", "abc123xyz", "abc      {tab}   I see dead people    my preciousssssss xyz".

Read (lots) more on: http://www.regular-expressions.info/repeat.html for repetitions, and ofcourse the other pages on www.regular-expressions.info or one of the other myriads of online resources.

Hope this cleared it up a bit (but I have a tendency to complicate matters if I start explaining stuff - if so then sorry :) )

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Very in depth information, gave me a lot to think about.
So basically the dot is the "wildcard" i thought * was.

The * is: zero or more repetitions of the preceding token

SO \s* means zero or more repetitions of the "group" /s meaning 0 or more repetitions of any whitespace character, yes?

So we test if the literal string '&' is between any quantity of whitespace before and after. Makes sense.

Much thanks.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Well, almost :)

The dot is a "wildcard" that stands for any character, but only exactly ONE instance of any character (read: not zero, not more than one, but exactly one). In regex, you can consider it a character class. So, .* literally means "exactly ONE character, and that zero or more times", which ends up being "zero or more of any character".

Yes, \s* means "a string of zero or more whitespace characters". It doesn't matter which whitespace characters or in which order they are. ({space} matches, {space}{space}{space}{tab}{tab}{space}{space}{tab}{space} also matches).

Yes, your conclusion is correct, but it is important to always realize that the * means zero or more (whereas the + means one or more). So your "any quantity" can also be zero. Which is why my snippet had a testcase for "kk&kk".

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Currently following this:

https://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial

A lot to look at.

Thanks once again.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

No problem :) Watch out though - rege̢x҉ has a tend̕e͡nçy to drive yo u͝ ą̀͟bsò̡͘lu̶ţ̡ ely s̀̕͢t̴̀̕a̷͠r͢͞k̕͜͝ r̨͘a̵v͠͏i̵ǹ̵g̸̶ bonkers a̵̴ǹ͞͏d̸͞  is al̡҉͝ş̛̕o̴̡ ̷̧͘ex͏̶҉t͟͞ré̢mé̢l̀͏͢y̧҉͟ ̵a̶̴̴d̸̛ḑ͞i͏ç̡̢t̵͠ìv͜e̸̸͡ . It pushes i͜ts̕el̀f ̨on you̕  like a hellish s͢͢͞͠͠c̵̛̕̕o͡͠u̶̢̕r͏̷̢͘ģ̴̴͠͠e͜͟ ̴̧͞͝͞o̸̕͝͏f̡͏̸͡ ͟e̷̵̡̡͜v̸͟͜͞i͏̨͟l̨͢ an d̴̕҉ ̕ma҉̕͡ke͞s͢ ̡i͠t̶̨se͟l̡f͝ ̀s͞͝͡ęe҉̀̕m͞ ͞li̵͡k͡e ̛́t̵̢h̸e͜ ̵́̕ only hammer you will ever need to solve any problem! Damn you regex, daaaaamn you alllll to hellllllll

 

W̷̡̡̪̤͕͉͙͎̳͋̎̓͑͐̑ͤ͆̎͊͟͠H̷̴̡̟̠͙͎͚̪͇̭̻̲̻͔͛̋ͧͣ͆̉͑̽́͌̄̓ͪ͘Y̵̴̗̘͚̞̦͈̮͈͚͈̖̍̔̂̇͡͞ͅ ̵̵̡̧͕̞͈̝̫͎̖̩͕̻͓̜̣̝͚͓̠̺͑ͬͮ͌̋ͮͪ̂ͧͫ͊ͤ̽͂͆̃͞R̒ͩ̂ͧ̊͒̿͛̿̽ͪ̿ͤ̒̐͒ͭͩͣ͜҉̸̘͓̭̗͙̯̠̼ͅĒ̢̫̜̘̱̠͇͕̘̦͔͖͎̫̻̦̂̅ͪ̃͛̕G̨̰͍̫̮̠̼͔̭͎̃ͬ̂̎ͦ͂ͦ̋ͯͩ̉̌̆̑̊̃̃͢͡E̥̣̺̜̹̞̲̗͖̮̗͊ͥ͌ͧͮ̂͋͑̈ͧͯ̃̾ͯ͘͞͞X̶̡̿͋͂ͧ̊̎ͨͬ͆̒͑̓̌͗̍̚͞͞͏̙̭͎͉͎̼̭ ̂̃̊͏͇̦̠͇̭͇̬̳̠̗̪̱̣͚̞̳̗́Ẇ̴̵̞̳̻̼̟͚̤̪̊̍̈̊̓ͧ̒̐̈́̇̕ͅH̷̥̹̼͙̮̮̻̯͉̹̞̰̙ͬͦͯ̑ͪͨ̌̽ͯ̍̉̓̈́̆͊̈̃̚͞͠͠Ÿ̢̞̘͖̼̰̠͉̞̻̙̯́͊ͭ͗̔̒͑̃̾͛ͦͅ

 

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Such odd Font. Nice.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Well, to be honest I stole it from this famous stackoverflow answer about parsing HTML with Regex. It is beautiful. It is well-put. It is immediately clear. It leaves no questions and it takes no prisoners. All my base are belong to it :) 

/edit: Actually I didn't steal the font but the idea :) I had this page handle the characters for me.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Really nice explanation of why not to use regex to parse HTML, i was mesmerized. :D Any tool to create such words?

 

 

̒ͩ̂ͧ̊͒̿͛̿̽ͪ̿ͤ̒̐͒ͭͩͣ͜҉̸̘͓̭̗͙̯̠̼ͅÔ͎̫̻̦̫̮̠̼͔̭͎̅ͪ̃͛̃ͬ̂̎ͦ͂ͦ̋ͯͩ̉̌̆̑̊̃̃̕͢͡Ṃ̺̜̹̞̲̗͖̮̗͊ͥ͌ͧͮ̂͋͑̈ͧͯ̃̾ͯ͘͞͞G̡̿͋͂ͧ̊̎ͨͬ͆̒͑̓̌͗̍̚͞!͞͏̙̭͎͉͎̼̭ ̂̃̊͏͇̦̠͇̭͇̬̳̠̗̪̱̣͚̞̳̗́

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Nice website, and nice tool, it's great to have the list of the possible parameters in the side, in case one forgets.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Well, the best website of the entire interwebz (except for this forum of course o:) ) is this: https://regex101.com

It is awesome for building and testing regular expressions and very nicely featured. There are some other sites but as far as I have seen nothing even comes close...

Check this out (not mine, but from the publicly available library): https://regex101.com/r/mIA28O/1

You gotta love that sh*t! :hyper: And it's free (well, you can donate but it is very modest about it) and you can save and publicize your regex work and use multiple flavors... It's just awesome.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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