Jump to content

Can someone help with a regexp


 Share

Recommended Posts

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.

Link to comment
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
Link to comment
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

Link to comment
Share on other sites

  • Moderators

@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

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.

Link to comment
Share on other sites

  • Moderators

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

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.

Link to comment
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)

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

Link to comment
Share on other sites

  • Moderators

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

Link to comment
Share on other sites

shhh, dont tell anyone about the helpfile

 

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

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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