nend

regex question [SOLVED]

15 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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

 

1 person likes this

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

Share this post


Link to post
Share on other sites

Yep!! Thanks, I was so close but I couldn't just made it work.

You helped me a lot!

Share this post


Link to post
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

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

BeatsPlayer - Music player.

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

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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
1 person likes this

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

Share this post


Link to post
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

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

BeatsPlayer - Music player.

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

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Share this post


Link to post
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".

1 person likes this

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

Share this post


Link to post
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

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

BeatsPlayer - Music player.

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

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Share this post


Link to post
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̷̥̹̼͙̮̮̻̯͉̹̞̰̙ͬͦͯ̑ͪͨ̌̽ͯ̍̉̓̈́̆͊̈̃̚͞͠͠Ÿ̢̞̘͖̼̰̠͉̞̻̙̯́͊ͭ͗̔̒͑̃̾͛ͦͅ

 

2 people like this

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

Share this post


Link to post
Share on other sites

Such odd Font. Nice.


Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

BeatsPlayer - Music player.

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

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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.

Share this post


Link to post
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

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

BeatsPlayer - Music player.

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

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Share this post


Link to post
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

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

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

BeatsPlayer - Music player.

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

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.

Firefox Profile Backup - Backup/restore previously saved profile.

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

Share this post


Link to post
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.

1 person likes this

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

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