Jump to content

StringRegExp problem


Go to solution Solved by avechuche,

Recommended Posts

First of all, sorry for my bad English :)

I need 2 things, first I need to check a username supports only lowercase letters, uppercase and point. [.a-zA-Z]
But I need that username, not start with it a period "."
^[^.][.a-zA-Z]+$
I have that expression, but not fully functional.
 
2) I need to check a number, but I need that number can not start with 0. I have this expression, but does not work very well ^[^0]d+$
 
Thx!
Edited by avechuche
Link to comment
Share on other sites

Forgot you wanted to match usernames with dots but without starting with a dot. Correction: ^[^.][.A-Za-z]+$

consolewrite("#1 '0321'=>"&StringRegExp("0321",'^[^0]\d+$')&@LF)
consolewrite("#2 '321'=>"&StringRegExp("321",'^[^0]\d+$')&@LF)
consolewrite("#3 '.usErname'=>"&StringRegExp(".usErname",'^[^\.][\.A-Za-z]+$')&@LF)
consolewrite("#4 'usErname'=>"&StringRegExp("usErname",'^[^\.][\.A-Za-z]+$')&@LF)
consolewrite("#5 'usErn.ame'=>"&StringRegExp("usErn.ame",'^[^\.][\.A-Za-z]+$')&@LF)

If in front of "=>" is a "1" = it matched, if is a "0" = failed to match.

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

Forgot you wanted to match usernames with dots but without starting with a dot. Correction: ^[^.][.A-Za-z]+$

consolewrite("#1 '0321'=>"&StringRegExp("0321",'^[^0]\d+$')&@LF)
consolewrite("#2 '321'=>"&StringRegExp("321",'^[^0]\d+$')&@LF)
consolewrite("#3 '.usErname'=>"&StringRegExp(".usErname",'^[^\.][\.A-Za-z]+$')&@LF)
consolewrite("#4 'usErname'=>"&StringRegExp("usErname",'^[^\.][\.A-Za-z]+$')&@LF)
consolewrite("#5 'usErn.ame'=>"&StringRegExp("usErn.ame",'^[^\.][\.A-Za-z]+$')&@LF)

If in front of "=>" is a "1" = it matched, if is a "0" = failed to match.

 

Try this 

ConsoleWrite("#2 ' 0321'=>" & StringRegExp(" 0321", '^[^0]\d+$') & @LF)

and

ConsoleWrite("#5 'usErn.ame'=>" & StringRegExp("m", '^[^\.][\.A-Za-z]+$') & @LF)

PD: 

Within [], no need to use "" to escape characters.
 
Ex ==> [?] = [?]
Edited by avechuche
Link to comment
Share on other sites

Forgot that kind of scenarios

^[^.]?[.A-Za-z]+$

^[^0]?d+$

both will fail if a space is added,

EDIT: those doesn't work since "?" is lazy, my bad

for what you proposed this works

^[^.][.A-Za-z]+$

^[^0]d+$

 

if you don't care about spaces being on your string

^[^.][.A-Za-zs]+$

[^0]?[ds]+$ ;Still mess up, it willl need lookahead RE

Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

Forgot that kind of scenarios

^[^.]?[.A-Za-z]+$

^[^0]?d+$

both will fail if a space is added,

EDIT: those doesn't work since "?" is lazy, my bad

for what you proposed this works

^[^.][.A-Za-z]+$

^[^0]d+$

 

if you don't care about spaces being on your string

^[^.][.A-Za-zs]+$

[^0]?[ds]+$ ;Still mess up, it willl need lookahead RE

 

 

The first was solved so
 
"^[a-zA-Z]+(.[a-zA-Z]+)?$"
 
ave.chuce ==> 1
avechuche ==> 1
.avechuche ==> 0

avechuche. ==> 0

[^0]?[ds]+$ It continues to resist.

Edited by avechuche
Link to comment
Share on other sites

ConsoleWrite("#2 '321'=>" & StringRegExp(" 0321", '^[\s][^0][\d\s]+) & @LF) = 0 OK
ConsoleWrite("#2 '321'=>" & StringRegExp("321", '^[\s][^0][\d\s]+) & @LF) = 0 BAD
ConsoleWrite("#2 '321'=>" & StringRegExp("321", '^[\s]?[^0][\d\s]+$') & @LF) = 1 OK
ConsoleWrite("#2 '321'=>" & StringRegExp(" 321", '^[\s]?[^0][\d\s]+$') & @LF) = 1 OK
ConsoleWrite("#2 '321'=>" & StringRegExp(" 0321", '^[\s]?[^0][\d\s]+) & @LF) = 1 DEM BAD

I guess I need some sleep, can't see what I'm doing wrong

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

  • Moderators

I'm not sure why we are including spaces now... but you can add the "s" wherever you see fit.

The neat thing about regex is that there are many ways to skin the cat.

Here's a method that wasn't used:

ConsoleWrite(( _
    StringRegExp("01234", "^(?!0)\d+\z") _
    = True) & @CRLF) ; fails as it should
ConsoleWrite(( _
    StringRegExp("12340", "^(?!0)\d+\z") _
    = True) & @CRLF) ; succeeds as it should

ConsoleWrite(( _
    StringRegExp(".userName", "^(?i)(?!\.)[\.a-z]+\z") _
    = True) & @CRLF) ; fails as it should
ConsoleWrite(( _
    StringRegExp("u.serName", "^(?i)(?!\.)[\.a-z]+\z") _
    = True) & @CRLF) ; succeeds as it should

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'm not sure why we are including spaces now... but you can add the "s" wherever you see fit.

The neat thing about regex is that there are many ways to skin the cat.

Here's a method that wasn't used:

ConsoleWrite(( _
    StringRegExp("01234", "^(?!0)\d+\z") _
    = True) & @CRLF) ; fails as it should
ConsoleWrite(( _
    StringRegExp("12340", "^(?!0)\d+\z") _
    = True) & @CRLF) ; succeeds as it should

ConsoleWrite(( _
    StringRegExp(".userName", "^(?i)(?!\.)[\.a-z]+\z") _
    = True) & @CRLF) ; fails as it should
ConsoleWrite(( _
    StringRegExp("u.serName", "^(?i)(?!\.)[\.a-z]+\z") _
    = True) & @CRLF) ; succeeds as it should

 

Great! You can add that you can not use a period at the end and I need to prevent two or more continuous point?

 
avechuche. ==> False.
ave..chuche ==> false
Edited by avechuche
Link to comment
Share on other sites

For the first, you can try wiht this : "^[a-zA-Z]+(.[a-zA-Z]+)*$"       or       ^([[:alpha:]]+)(.(?1))*$

It should match something like User.Na.me, UserName ....

For the second : "^[1-9]d*$"

Edited by jguinch
Link to comment
Share on other sites

From the help file under 'Datatypes' we have,
"A value 0 will be equal to Boolean False
Any other number value will be equal to Boolean True."

So in the example, if the first regex is true then the first regex does not equal zero.. Therefore, the first expression 'regex <> 0' is true.

If the second regex is false (meaning there are no two dots next to each other) then the second regex  equals zero.   Therefore, the second expression 'regex = 0' is true.

Now when the  first 'regex <> 0' is True And the second 'regex = 0' is true the outcome of the two expressions combined with And  is true
If either or both expressions are false then the combined outcome is false.

Local $aList[4] = ["u.serName", ".userName", "userName.", "u..serName"]

For $i = 0 To UBound($aList) - 1
    ConsoleWrite(((StringRegExp($aList[$i], "^(?i)(?!\.)[\.a-z]+(?<!\.)\z") <> 0) And _
                  (StringRegExp($aList[$i], "\.\.") = 0)) & @TAB & $aList[$i] & @CRLF)
Next

#cs Returns :-
True    u.serName
False    .userName
False    userName.
False    u..serName
#ce

 

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