Jump to content

Help understanding StringRegExp.


ReconX
 Share

Recommended Posts

So, every time I try to understand the formulas for StringRegExp, my brain goes to mush. Someone on the forum helped me with a previous question I had, and wrote this piece of code for me. I set out to understand it, and hopefully have. I've written what I  think the code means. It was to help separate "Movie (2002)" to two separate strings being "Movie" and "2002". The code in full is, "([^\\]*?)\h*\((\d+)\)".

([^\\]*?)

( = Opens capture group. 
[ = Opens Expression.
^ = Matches any character not in the set. Meaning anything besides "\".
\ = Determines Metacharacter.
\ = Looks for character "\" determined by the previous "\".
] = Closes Expression
*? = More Lazy
) = Closes capture group.

\h*

\h = Matches horizontal whitespace characters.
* = More greedy. 

\((\d+)\)

\ = Determines Metacharacter.
( = Looks for character "(" determined by the previous "\".
( = Opens capture group. 
\d = Looks for digits 0-9.
+ = More greedy
) = Closes capture group
\ = Determines Metacharacter.
) = Looks for character ")" determined by the previous "\".
 

Am I on the correct path?

Edited by ReconX
Link to comment
Share on other sites

it seems to do the thing. 

However, if you just want two groups "date / everything else" and if all of your titles have a parenthetical 4 digit year at the end of the string, then i would be specific about the number of digits and end of the string.  Scoops up a bunch of edge cases with a small modification.
 

$s = "Movie (0000) (part1000) (2002)"

;~ $a = stringregexp($s , "([^\\]*?)\h*\((\d+)\)" , 3)

;~ _ArrayDisplay($a)

$a = stringregexp($s , "(.*?)(\(\d{4}\)\z)" , 3)

_ArrayDisplay($a)

 

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

Link to comment
Share on other sites

7 minutes ago, iamtheky said:

Scoops up a bunch of edge cases with a small modification

To just add a little '$' to the previous expression was not good enough ? :idiot:

#Include <Array.au3>

$s = "Movie (0000) (part1000) (2002)"
$a = stringregexp($s , "([^\\]*?)\h*\((\d+)\)$" , 3)
_ArrayDisplay($a)

 

@ReconX

The best way is to translate the expression in common language , i.e. ([^\\]*?) means : "please capture 0 or more non-backslash characters (lazy way)" , or \((\d+)\) means : "please capture one or more digits enclosed with parenthesis" :)

Link to comment
Share on other sites

you know i dont know what any of those characters mean.  if i cant .*? it, i use yours. 

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

Link to comment
Share on other sites

You know - in the help file there is the "Tutorial - Regular Expression" and at the bottom is a button with a that will open a script in SciTE. Run that script and you will find it to be a GUI for testing various StringRegExp() patterns. Looks like this:

image.png.4e901b1f3dc341c76a15ccb223740a33.png

Edited by Bert
Link to comment
Share on other sites

15 hours ago, iamtheky said:

you know i dont know what any of those characters mean

Hmm I also know that this assertion is not exactly true  ;)

But I'm surprised you didn't suggest such a solution

$s = "Movie (0000) (part1000) (2002)"
$r = StringSplit(StringMid($s, 1, StringInStr($s, "(", 0, -1)-1) & @crlf & StringMid($s, StringInStr($s, "(", 0, -1)+1, 4), @crlf, 3)
_ArrayDisplay($r)

:P

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