dusty071 Posted January 27, 2015 Posted January 27, 2015 Im terrible at regular expressions, i have no idea what im doing. Example random string "ndbs vsdvbnfA1B2-Y6W3-ASD5-JFG0-7WE2 SbubfU", i need to extract the clean string A1B2-Y6W3-ZSD5-JFG0-7WE2 and store it in a new variable. Help much appreciated.
Malkey Posted January 27, 2015 Posted January 27, 2015 A "clean string" is a bit vague. The more examples showing all the requirements of the before and after strings, the better the RE pattern. Here's a start. Local $sRandomString = "ndbs vsdvbnfA1B2-Y6W3-ASD5-JFG0-7WE2 SbubfU" Local $sNewVariable = StringRegExpReplace($sRandomString, "^.*?([A-Z0-9\-]{9,}+).*$", "\1") ConsoleWrite($sNewVariable & @LF); Returns A1B2-Y6W3-ASD5-JFG0-7WE2
AutoRun Posted January 27, 2015 Posted January 27, 2015 #include <Array.au3> local $_str = "ndbs vsdvbnfA1B2-Y6W3-ASD5-JFG0-7WE2 SbubfUndbs vsdvbnfA1B2-Y1W3-ASD5-JFG0-7WE2 SbubfUndbs vsdvbnfA1Q2-Y6S3-ASD5-JFG0-7WS2 SbubfU" ; Your string local $_iStart = 1, $_arFoundExp[1], $i = 0 While @error = 0 And $_iStart <> 24 $_arMatches = StringRegExp($_str, "([A-z0-9]{4}-){4}[A-z0-9]{4}", 2, $_iStart) If @error <> 1 Then $_iStart = StringInStr($_str, $_arMatches[0], 0, 1, $_iStart) + 24 _ArrayAdd($_arFoundExp, $_arMatches[0]) $_arFoundExp[0] = $_arFoundExp[0] + 1 EndIf WEnd If $_arFoundExp[0] <> 0 Then ; If array with a found matches isn't empty _ArrayDisplay($_arFoundExp, "List of the Matches") ; They will be here EndIf Hello! Put it in your SciTE then change the variable $_str to your string and try
Moderators SmOke_N Posted January 27, 2015 Moderators Posted January 27, 2015 (edited) @AutoRun Would this give the same effect? "([A-z0-9]{4}(?:-|$|h)){5}" Edit: BTW, that pattern is invalid. Run this as the $_str: local $_str = "ndbs vsdvbnfA1B2-Y6W3-ASD5-JFG0-7WE2 SbubfUndbs vsdvbnfA_1B2-Y1W3-ASD5-JFG0-7WE2 SbubfUndbs vsdvbnfA1Q2-Y6S3-ASD5-JFG0-7wS2 SbubfU" ; Your string I would think more like this: "(?i)([a-z0-9]{4}(?:$|h|-)){5}" Edited January 27, 2015 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Moderators SmOke_N Posted January 27, 2015 Moderators Posted January 27, 2015 (edited) Thought about this regex (AutoRuns one) while I was at the store. I think this would be simplified: #include <Array.au3> local $_str = "ndbs vsdvbnfA1B2-Y6W3-ASD5-JFG0-7WE2 SbubfUndbs vsdvbnfA_1B2-Y1W3-ASD5-JFG0-7WE2 SbubfUndbs vsdvbnfA1Q2-Y6S3-ASD5-JFG0-7wS2 SbubfU" ; Your string Local $aReg = StringRegExp($_str, "(?i)((?:[a-z0-9]{4}(?:$|\h|-)){5})", 3) _ArrayDisplay($aReg) Edited January 27, 2015 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
iamtheky Posted January 27, 2015 Posted January 27, 2015 i think also.. #include <Array.au3> local $_str = "ndbs vsdvbnfA1B2-Y6W3-ASD5-JFG0-7WE2 SbubfUndbs vsdvbnfA_1B2-Y1W3-ASD5-JFG0-7WE2 SbubfUndbs vsdvbnfA1Q2-Y6S3-ASD5-JFG0-7wS2 SbubfU" ; Your string Local $aReg = StringRegExp($_str, "[^\W_]{4}-[^\W_]{4}-[^\W_]{4}-[^\W_]{4}-[^\W_]{4}", 3) _ArrayDisplay($aReg) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
Moderators SmOke_N Posted January 27, 2015 Moderators Posted January 27, 2015 @boththose... neat... simplified: "((?:[^W_]{4}-){4}[^W_]{4})" Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
iamtheky Posted January 27, 2015 Posted January 27, 2015 shhh, dont tell anyone about the helpfile [:alnum:] ASCII letters and digits (same as [^W_] ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
dusty071 Posted January 28, 2015 Author Posted January 28, 2015 Thanks to you all, my brain cant handle regexp.
AutoRun Posted January 29, 2015 Posted January 29, 2015 (edited) You are right my regexp is a little silly. My first idea was StringRegExp($_str, "(([A-z0-9]{4}-){4}[A-z0-9]{4})", 3) But it have a two capturing group. My regexp knowledge goes from JavaScript and before now I didn't know about the non-capturing group (? Thanks for it. And of course (?:[^W]{4}-) is better. Why you used "_" in [^W_]? Edited January 29, 2015 by AutoRun
jguinch Posted January 29, 2015 Posted January 29, 2015 StringRegExp($_str, "((?:[A-z0-9]{4}-){4}[A-z0-9]{4})", 3) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
mikell Posted January 29, 2015 Posted January 29, 2015 (edited) And of course (?:[^W]{4}-) is better. No it is not. You're looking for uppercase and/or digits but [^W] and [A-z] will match lowercase letters [A-Zd] or [A-Z0-9] will match digits and uppercase letters only Edited January 29, 2015 by mikell
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