Lupo73 Posted November 15, 2009 Share Posted November 15, 2009 I'm trying to create a function that check if a string (as a file name) match with a particular pattern defined by the user. At the moment I used this simply couple of lines: $pattern = StringReplace($str, "*", "(.*?)") $match = StringRegExp($item, $pattern) $match results 1 if strings match and 0 otherwise. It works fine, but now I have found a problem: if for example is $str = "*.txt" the StringRegExp function return 1 for both $item = "item.txt" and $item = "item.txt.zip" (and so even if it doesn't correctly match). Instead I need that if is "*.txt*" it is correct that both match (because the check is made with "(.*?).txt(.*?)"), but if there isn't the second asterisk, the match may return 0. How can I resolve this issue? Thanks! SFTPEx, AutoCompleteInput, _DateTimeStandard(), _ImageWriteResize(), _GUIGraduallyHide(): some AutoIt functions. Lupo PenSuite: all-in-one and completely free selection of portable programs and games. DropIt: a personal assistant to automatically manage your files. ArcThemALL!: application to multi-archive your files and folders. Link to comment Share on other sites More sharing options...
Authenticity Posted November 15, 2009 Share Posted November 15, 2009 (edited) Use anchors. $pattern = StringReplace($str, "*", "(.*?)") & "$" $match = StringRegExp($item, $pattern) ...so it matches only at the end of the string, or "\b" to match at a word boundary. Or maybe something more restrictive, "(?i)[^a-z0-9_.]", etc.. On the other side, .*? can match easily file names such as ".txt" and "..txt" but it's something else. Edited November 15, 2009 by Authenticity Link to comment Share on other sites More sharing options...
Lupo73 Posted November 16, 2009 Author Share Posted November 16, 2009 It works fine, but now I need to do the same if the pattern is for example something*.*In this case I can't add $ at the pattern beginning, but it works wrongly because both "something2.zip" and "2something.zip" match fine (but the second string may not match).Sorry, but the pattern system is a little complex (for example adding $ to limit the checking is not reported in Help). What I need is to make a normal pattern checking, that allow extra characters only under * and not also at the begin or the end of a string. Thanks for the help SFTPEx, AutoCompleteInput, _DateTimeStandard(), _ImageWriteResize(), _GUIGraduallyHide(): some AutoIt functions. Lupo PenSuite: all-in-one and completely free selection of portable programs and games. DropIt: a personal assistant to automatically manage your files. ArcThemALL!: application to multi-archive your files and folders. Link to comment Share on other sites More sharing options...
GEOSoft Posted November 16, 2009 Share Posted November 16, 2009 (edited) According to the help file "$" matches at the end of the string and "^" matches at the beginning of the string. (?i)(\b\w+\.[[:alnum:]]+\b Edit: Correction in the SRE Edited November 16, 2009 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 More sharing options...
Lupo73 Posted November 16, 2009 Author Share Posted November 16, 2009 Good, this solution seems to work fine: $pattern = StringReplace($str, "*", "(.*?)") $match = StringRegExp($item, "^" & $pattern & "$") What is the purpose of this string you have wrote? (?i)(\b\w+\.[[:alnum:]]+\b SFTPEx, AutoCompleteInput, _DateTimeStandard(), _ImageWriteResize(), _GUIGraduallyHide(): some AutoIt functions. Lupo PenSuite: all-in-one and completely free selection of portable programs and games. DropIt: a personal assistant to automatically manage your files. ArcThemALL!: application to multi-archive your files and folders. Link to comment Share on other sites More sharing options...
GEOSoft Posted November 16, 2009 Share Posted November 16, 2009 Good, this solution seems to work fine: $pattern = StringReplace($str, "*", "(.*?)") $match = StringRegExp($item, "^" & $pattern & "$") What is the purpose of this string you have wrote? (?i)(\b\w+\.[[:alnum:]]+\b (?i) = Case-insensitive \b = match at word boundary \w+ = match any word characters (a-z0-9_) one or more times \. = match a literal "." [[:alnum:]]+ = match any alpha-numeric (a-z0-9) characters 1 or more times \b = match at word boundary 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 More sharing options...
Lupo73 Posted November 17, 2009 Author Share Posted November 17, 2009 Uhm.. and I could use it instead of the actual mode? Can you insert it in my example? Thanks SFTPEx, AutoCompleteInput, _DateTimeStandard(), _ImageWriteResize(), _GUIGraduallyHide(): some AutoIt functions. Lupo PenSuite: all-in-one and completely free selection of portable programs and games. DropIt: a personal assistant to automatically manage your files. ArcThemALL!: application to multi-archive your files and folders. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now