Jump to content

regex to find 3 numbers and 4 letters


 Share

Recommended Posts

there will be many strings, of varying length and structure I want the computers that match the scheme of

"XX-" followed by 4 letters and 3 numbers in any arrangement

XX-EXCHG01 - does not match

XX-GLG65J4 - matches

XY-GLG65J4 - does not match

XX-4JG5L6G - matches

COMPUT3R - does not match

 

 

heres the long way:

msgbox(0, '' , _TestString("XX-Hel3l45"))
msgbox(0, '' , _TestString("XY-Hel3l45"))
msgbox(0, '' , _TestString("XX-Hel3l45678"))
msgbox(0, '' , _TestString("XX-Hel3l45"))

Func _TestString($sTest)

$aTest = (stringleft($sTest , 3) = "XX-") ? stringsplit(stringtrimleft($sTest, 3) , "" , 2) : execute("return 0")


$nCount = 0
$aCount = 0

If UBound($aTest) > 7 Then return 0

for $i = 0 to ubound($aTest) - 1
 $nCount += StringIsDigit($aTest[$i])
 $aCount += StringIsAlpha($aTest[$i])

 If $i = ubound($aTest) - 1 AND $nCount = 3 AND $aCount = 4 Then return $sTest

Next

EndFunc

 

Edited by iamtheky

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

Link to comment
Share on other sites

iamtheky,

#include <array.au3>

Local $str = 'XX-EXCHG01,XX-GLG65J4,XY-GLG65J4,XX-4JG5L6G,COMPUT3R'

Local $aOriginal = StringSplit($str, ',', 3)

For $1 = 0 To UBound($aOriginal) - 1
    If _test($aOriginal[$1]) Then
        ConsoleWrite($aOriginal[$1] & ' matches' & @CRLF)
    Else
        ConsoleWrite($aOriginal[$1] & ' does not match' & @CRLF)
    EndIf
Next

Func _test($str)

    If StringLeft($str, 3) <> 'XX-' Then Return False
    $str = StringRegExpReplace($str, '(?i)xx-(.*)', '$1')
    $str = StringRegExpReplace($str, '\d', '', 3)
    $str = StringRegExpReplace($str, '[A-Za-z]', '', 4)
    If StringLen($str) = 0 Then
        Return True
    Else
        Return False
    EndIf

EndFunc   ;==>_test

kylomas

p.s. I'm sure the code can be optimized, just an example...

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

Link to comment
Share on other sites

oooh, but you have regexps, ive seen people with the power to combine those into magical one-liners.

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

Link to comment
Share on other sites

5 minutes ago, iamtheky said:

ive seen people with the power to combine those into magical one-liners.

Yea, but I'm not one of them...forgot about ternary construct...

#include <array.au3>

Local $str = 'XX-EXCHG01,XX-GLG65J4,XY-GLG65J4,XX-4JG5L6G,COMPUT3R'

Local $aOriginal = StringSplit($str, ',', 3)

For $1 = 0 To UBound($aOriginal) - 1
    ConsoleWrite($aOriginal[$1] & (_test($aOriginal[$1]) ? ' matches' : ' does not match' ) & @CRLF)
Next

Func _test($str)

    If StringLeft($str, 3) <> 'XX-' Then Return False
    $str = StringRegExpReplace($str, '(?i)xx-(.*)', '$1')
    $str = StringRegExpReplace($str, '\d', '', 3)
    $str = StringRegExpReplace($str, '[A-Za-z]', '', 4)

    return ( stringlen($str = 0 ) ? true : false )

EndFunc   ;==>_test

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

Link to comment
Share on other sites

One-line regex :

Local $str = 'XX-EXCHG01,XX-GLG65J4,XY-GLG65J4,XX-4JG5L6G,COMPUT3R'

Local $aOriginal = StringSplit($str, ',', 3)

For $i = 0 To UBound($aOriginal) - 1
    ConsoleWrite($aOriginal[$i] & (_TestString($aOriginal[$i]) ? ' matches' : ' does not match' ) & @CRLF)
Next


Func _TestString($sTest)
    Return StringRegExp($sTest, "^XX-(?=(?:\d*[A-Z]){4})(?=(?:[A-Z]*\d){3})[A-Z0-9]{7}$")
EndFunc

 

Link to comment
Share on other sites

So much to learn in that one line.  Well done.

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

Link to comment
Share on other sites

I think you can do a simple version using StringRegExpReplace+@extended.

 

Saludos

Link to comment
Share on other sites

@Danyfirex : StringRegExpReplace + @extended  was my first idea :P

Local $str = 'XX-EXCHG01,XX-GLG65J4,XY-GLG65J4,XX-4JG5L6G,COMPUT3R'

Local $aOriginal = StringSplit($str, ',', 3)

For $i = 0 To UBound($aOriginal) - 1
    ConsoleWrite($aOriginal[$i] & (_TestString($aOriginal[$i]) ? ' matches' : ' does not match' ) & @CRLF)
Next



Func _TestString($sTest)
    Local $s = StringRegExpReplace($sTest, "^XX-", "")
    If Not @extended Then Return 0

    $s = StringRegExpReplace($s, "\d", "")
    If @extended <> 3 Then Return 0

    $s = StringRegExpReplace($s, "[A-Z]", "")
    If @extended <> 4 Then Return 0

    Return StringLen($s) = 0
EndFunc

 

Link to comment
Share on other sites

:PYes kylomas, pretty close, except that mine does not return True for a string like XX-ABCDEFGHI123456789 :D

(I see you wrote too fast : stringlen($str = 0 )  => stringlen($str) = 0  )

Link to comment
Share on other sites

17 minutes ago, jguinch said:

:PYes kylomas, pretty close, except that mine does not return True for a string like XX-ABCDEFGHI123456789 :D

(I see you wrote too fast : stringlen($str = 0 )  => stringlen($str) = 0  )

Yes, corrected, 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

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