Jump to content

stringregexp match


Recommended Posts

hello world

i am currently using this

$aDate = StringRegExp($date, "(?i)(\V+)(\d{8})\V(\d{4})", 3)

to successfullly match these

$string = "IMG00136-20100524-0109"
$string = "IMG00136_20100524_0109"
$string = "IMG_20000526_100019_402"
$string = "IMG-20120615-00028"
$string = "IMG_20120615_00028"
$string = "Texas-20111117-00060"
$string = "Texas_20111117_00060"
$string = "Southwest San Marcos Valley-20111110-00046"
$string = "Southwest San Marcos Valley_20111110_00046"
$string = "Long Island-Laketown-20110526-00023"
$string = "Long Island-Laketown_20110526_00023"

however these do not match unless i add an \A at the beginning (but if i do that then the ones above do not match)

$string = "20141119_193702"
$string = "20141119-193702" 

any way to match both?

thanks in advance !

Link to comment
Share on other sites

You might want something like this

#Include <Array.au3>

$string = ""
$string &= "IMG00136-20100524-0109" & @crlf
$string &= "IMG00136_20100524_0109" & @crlf
$string &= "IMG_20000526_100019_402" & @crlf
$string &= "IMG-20120615-00028" & @crlf
$string &= "IMG_20120615_00028" & @crlf
$string &= "Texas-20111117-00060" & @crlf
$string &= "Texas_20111117_00060" & @crlf
$string &= "Southwest San Marcos Valley-20111110-00046" & @crlf
$string &= "Southwest San Marcos Valley_20111110_00046" & @crlf
$string &= "Long Island-Laketown-20110526-00023" & @crlf
$string &= "Long Island-Laketown_20110526_00023" & @crlf
$string &= "20141119_193702" & @crlf
$string &= "20141119-193702" 

$aDate = StringRegExp($string, "(?i)(\V*)(\d{8})\V(\d{4})", 4)  ; $STR_REGEXPARRAYGLOBALFULLMATCH 
Local $res[UBound($aDate)][3]
For $i = 0 to UBound($aDate)-1
   $res[$i][0] = ($aDate[$i])[1]
   $res[$i][1] = ($aDate[$i])[2]
   $res[$i][2] = ($aDate[$i])[3]
Next
 _ArrayDisplay($res)

:)

Link to comment
Share on other sites

best example for magic number 4 I have seen, that needs its own thread.  Even moreso with the direct access of an array in an array.

Seriously Mikell, i think you have most certainly added back into routine consideration, a storage option I had disregarded for the most part as being cumbersome compared to alternatives.  

I would call this shorthand, but is there a proper name for that type of syntax, that would be a fitting title for a thread consisting of examples?

Edited by boththose

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

Link to comment
Share on other sites

About alternatives you probably mean this commonly used one

$aDate = StringRegExp($string, "(?i)(\V*)(\d{8})\V(\d{4})", 3)  ; $STR_REGEXPARRAYGLOBALMATCH
Local $res[UBound($aDate)/3][3]
For $i = 0 to UBound($res)-1
   $res[$i][0] = $aDate[$i*3]
   $res[$i][1] = $aDate[$i*3+1]
   $res[$i][2] = $aDate[$i*3+2]
Next
 _ArrayDisplay($res)

But I personally prefer the flag 4 regex
Referring to the last example in the helpfile this flag 4 way could be written like this

$aDate = StringRegExp($string, "(?i)(\V*)(\d{8})\V(\d{4})", 4)  ; $STR_REGEXPARRAYGLOBALFULLMATCH 
Local $res[UBound($aDate)][3]
For $i = 0 to UBound($aDate)-1
   $aMatch = $aDate[$i]
   $res[$i][0] = $aMatch[1]
   $res[$i][1] = $aMatch[2]
   $res[$i][2] = $aMatch[3]
Next
 _ArrayDisplay($res)

Concerning the ($aDate[$i])[1] syntax I don't know where it's mentioned in the helpfile
I saw it once in a topic and found it very useful and handy so I kept this tip stored into a couple of little grey cells for further use  :)

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