rcmaehl Posted January 14, 2018 Posted January 14, 2018 Hi all, I still suck at regex as always and I need some help. According to the regex tester I normally use this should be working fine but it doesn't.... StringRegExp($sString, "\A[1-9]+[0-9]*(\-[1-9]+[0-9]*)?,*\Z") I basically want to match: all numbers EXCEPT 0, but including 10, 20, etc with each number separated by a comma and allowing a "-" separated range as a value For example: 1-5,7,10-12 I've spent a couple hours modifying it but I'm not sure where I've gone wrong. Any help would be appreciated! My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated. My Projects WhyNotWin11, MSEdgeRedirect Cisco Finesse, Github, IRC UDF, WindowEx UDF
mikell Posted January 14, 2018 Posted January 14, 2018 ? StringRegExp($str, '^([1-9]+\d*(-[1-9]+\d*)?,?)+$') rcmaehl 1
jguinch Posted January 14, 2018 Posted January 14, 2018 StringRegExp($sString, "^(([1-9]\d*)(?:-(?2))?)(?:,(?1))*$") rcmaehl 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
rcmaehl Posted January 14, 2018 Author Posted January 14, 2018 15 minutes ago, mikell said: ? StringRegExp($str, '^([1-9]+\d*(-[1-9]+\d*)?,?)+$') 12 minutes ago, jguinch said: StringRegExp($sString, "^(([1-9]\d*)(?:-(?2))?)(?:,(?1))*$") Thanks you all. I think I see where I went wrong! My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated. My Projects WhyNotWin11, MSEdgeRedirect Cisco Finesse, Github, IRC UDF, WindowEx UDF
iamtheky Posted January 14, 2018 Posted January 14, 2018 (edited) What is the most efficient way to ensure that the number after the hyphen is greater than the number before it? As if you were passing that range to arraydelete or similar where it would be a requirement... #include<array.au3> local $arr = [0,1,2,3,4,5,6,7,8,9,10] $sString = "1-5,7,8-9" msgbox(0, '' , StringRegExp($sString, "^(([1-9]\d*)(?:-(?2))?)(?:,(?1))*$")) msgbox(0, '' , StringRegExp($sString, '^([1-9]+\d*(-[1-9]+\d*)?,?)+$')) _ArrayDelete($arr , stringreplace($sString , "," , ";")) _ArrayDisplay($arr) Edited January 14, 2018 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
jchd Posted January 15, 2018 Posted January 15, 2018 2 hours ago, mikell said: StringRegExp($str, '^([1-9]+\d*(-[1-9]+\d*)?,?)+$') Fails. Misses when the final number is > 9 and is followed by - and another number. For instance, 1-34-5 is matched as 1-3 then 4-5. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
kylomas Posted January 15, 2018 Posted January 15, 2018 Is it not this simple?... #include <Array.au3> Local $str = '1-34,1-5,7,10-12' Local $aret = StringRegExp($str, '([\d\-]+)(?:,|$)', 3) _ArrayDisplay($aret) ...ready for my dose of humble pie... kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
iamtheky Posted January 15, 2018 Posted January 15, 2018 (edited) that scoops up 0 and negatives #include <Array.au3> Local $str = '0,1-5,-7,10-12' msgbox(0, '' , StringRegExp($str, '([\d\-]+)(?:,|$)')) Edited January 15, 2018 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
kylomas Posted January 15, 2018 Posted January 15, 2018 @iamthekey - I see it now, thanks Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
mikell Posted January 15, 2018 Posted January 15, 2018 10 hours ago, jchd said: Fails. Misses when the final number is > 9 and is followed by - and another number. For instance, 1-34-5 is matched as 1-3 then 4-5. It doesn't fail if used with the $STR_REGEXPMATCH flag for a simple match/nomatch as intended in post #1 The pattern is inappropriate indeed to extract the results but it was not the initial question
jguinch Posted January 15, 2018 Posted January 15, 2018 @mikell : #include <StringConstants.au3> Local $str = "7-12-15" MsgBox(0, "", StringRegExp($str, '^([1-9]+\d*(-[1-9]+\d*)?,?)+$', $STR_REGEXPMATCH) ) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
AspirinJunkie Posted January 15, 2018 Posted January 15, 2018 (edited) Just another approach: #include <Array.au3> Local $str = '0,1-5,-7,3-4-5,10-12' $a_RE = StringRegExp($str, '(?:\A|,)([1-9]\d*(?>-[1-9]\d*)?)(?=(?>\Z|,))', 3) _ArrayDisplay($a_RE) Edit: Ah - i misunderstood a little bit: You don't want to match - you wan't to check the whole string - right? Then this is my approach: #include <Array.au3> Local $str = '10,1-5,7,3-5,10-12' MsgBox(0,"", StringRegExp($str, '\A(?>[1-9]\d*(?>-[1-9]\d*)?,?)*\Z')) Edit2: Hm - quite the same as mikell's Edited January 15, 2018 by AspirinJunkie
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