Sign in to follow this  
Followers 0
dusty071

Can someone help with a regexp

12 posts in this topic

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.

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

#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

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

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)

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

Share this post


Link to post
Share on other sites

@boththose... neat... simplified:

"((?:[^W_]{4}-){4}[^W_]{4})"


[center]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.[/center]

Share this post


Link to post
Share on other sites

shhh, dont tell anyone about the helpfile

 

[:alnum:] ASCII letters and digits (same as [^W_]

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

Share this post


Link to post
Share on other sites

Thanks to you all, my brain cant handle regexp.

Share this post


Link to post
Share on other sites

#10 ·  Posted (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 by AutoRun

Share this post


Link to post
Share on other sites

#12 ·  Posted (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 by mikell

Share this post


Link to post
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
Sign in to follow this  
Followers 0