Sign in to follow this  
Followers 0
MyEarth

Special Char

21 posts in this topic

I want to check if a string has special char ( all the special char like .+*@ etc., i want only numbers and letters and nothing else ) so i have try with StringInString but seems not work

$string = "fdaafsdfsafa@"
$a = StringInStr($string, '!"£$%/\(|)=?^ìè+òàù,.-é*ç°§;:_@#')
ConsoleWrite($a)

I don't want to replace it, but give an error if found a special char in the string

Thanks

Share this post


Link to post
Share on other sites



It might be even easier to specify what character range are allowable in the regexp.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Pratically in StringRegEx i need to use the: 

[\W]

For matching any special char? I'd like to have only letters and numbers in my string, or maybe i can do the opposite and search for digit and letters?

Edited by MyEarth

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

@jchd,

Un français ! La communauté francophone est bien présente, ça fait plaisir :)

perhaps, somethings like this :

$sStr = "fdaafsdfsaf@a"
$iResult = StringRegExp($sStr, "^(?-i)[a-z\s0-9]*$")

if $iResult Then
    Msgbox(0,"","Value OK")
Else
    Msgbox(0,"","Value KO")
EndIf
Edited by M4n0x

Share this post


Link to post
Share on other sites

"i want only numbers and letters and nothing else" - StringIsAlNum() should do it.

Share this post


Link to post
Share on other sites

Apparently, the regexp matching character "W" and the 'StringIsAlNum' function both allow the underscore character "_" in their list of alphanumeric characters.

@ M4n0x
"(?-i)" is the default and therefore an unnecessary case-sensitivity flag.  So your example is calling the capital A to Z letters unwanted special characters.
You probably meant to use "(?i)" for case-insensitivity.


This example below is labelling all non-letters (upper and lower case) and non-numbers as unwanted special characters.
Other unwanted special characters include the underscore, horizontal and vertical whitespace characters, and a decimal point or fullstop.

Local $string = "fBa0a1fAsd_fs2a"
If StringRegExp($string, '[^a-zA-Z0-9]') Then
    MsgBox(0, "Results", "String has a special character", 3)
Else
    MsgBox(0, "Results", "String does not have any special characters present.", 3)
EndIf

Share this post


Link to post
Share on other sites

M4n0X,

There are even more French users on the French forum!


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

@Malkey,

apologize ! :

i would meant "(?i)" for insensitive case as you say.

so, corrected here, thanks :)

$sStr = "fdaafsdfk#safa"
$iResult = StringRegExp($sStr, "^(?i)[a-z0-9]*$")
if $iResult Then
Msgbox(0,"","Value OK")
Else
    Msgbox(0,"","Value KO")
EndIf

Share this post


Link to post
Share on other sites

 

@Malkey,

apologize ! :

i would meant "(?i)" for insensitive case as you say.

so, corrected here, thanks :)

$sStr = "fdaafsdfk#safa"
$iResult = StringRegExp($sStr, "^(?i)[a-z0-9]*$")
if $iResult Then
Msgbox(0,"","Value OK")
Else
    Msgbox(0,"","Value KO")
EndIf

Or Just:

$sStr = "fdaafsdfk#safa"
$iResult = StringRegExp($sStr, "^[A-z0-9]*$")
if $iResult Then
Msgbox(0,"","Value OK")
Else
    Msgbox(0,"","Value KO")
EndIf

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I'm confused, which one i need to use?

For what i have understand:

^[A-z] is equal to [^a-zA-Z]

Match any "word" character, lower or upper. Right?

But what does mean:

*$

I can't see nothing in the help about the dollar simbol

Edited by MyEarth

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I'm confused, which one i need to use?

For what i have understand:

^[A-z] is equal to [^a-zA-Z]

Match any "word" character, lower or upper. Right?

But what does mean:

*$

I can't see nothing in the help about the dollar simbol

No, outside of [], ^ matches the start of the string. Word characters are A-z 0-9 and underscore

Basically you are matching the start of the string (^), followed by zero or more (that's what the star does) alphanumeric characters ([A-z0-9], followed by the end of the string ($).

 

This can only match if all the characters in the string are alphanumeric characters.

Edited by Mat

Share this post


Link to post
Share on other sites

Ok, understood. I'll check better the helpfile, for now i think i'll use the Malkey's script, i don't want to allow the underscore in the string

Strange the point is a special char but the underscore not

Share this post


Link to post
Share on other sites

Beware: [A-z] includes [ ] ^ _ ` which are not wanted IFUC.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

underscore is not alpha-numeric and will make StringIsAlNum("_") return 0. try it.

Share this post


Link to post
Share on other sites

underscore is not alpha-numeric and will make StringIsAlNum("_") return 0. try it.

Correct, but if you try with special characters as "éèéàöüä" it doesn't work.

Share this post


Link to post
Share on other sites

Correct, but if you try with special characters as "éèéàöüä" it doesn't work.

 

these are alpha-numeric, although not in English. i doubt that the original intention of the requester is to exclude non-English strings.

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Maybe i wasn't clear. I want a string with:

0123456789

abcdefghijklmnopqrstuvwxyz

ABCDEFGHIJKLMNOPQRSTUVWXYZ

I want to avoid any special character and non printable character. I'm usign the Malkey's script and check the help of StringRegEx

Edited by MyEarth

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Here is a script to test all characters individually to see if they are special characters or not.

; Testing the StringIsAlNum() function
; A returns of "1" means all the characters in string being tested are only alphanumeric characters.
; A returns of "0" means a non-alphanumeric character is in the string being tested.
ConsoleWrite("éèéàöüä  returns " & StringIsAlNum("éèéàöüä") & @LF) ; returns 1
; Note: The underscore character is not an alphanumeric character, contrary to what I said in Post #7.

ConsoleWrite("---- StringIsAlNum() ----" & @LF)
For $i = 0 To 255
    ConsoleWrite(">" & $i & " = " & @TAB & "0x" & Hex($i, 2) & " " & ChrW($i) & @TAB & StringIsAlNum(ChrW($i)) & @LF)
Next

ConsoleWrite(@LF & "---- StringRegExp($string, '[^a-zA-Z0-9]') ----" & @LF) ; From post #7
For $i = 0 To 255
    ConsoleWrite(">" & $i & "  " & ChrW($i) & @TAB & StringRegExp(ChrW($i), '[^a-zA-Z0-9]') & @TAB & _ ; Returns "0" (False) if tested character is a-z, A-Z, or 0-9
            (StringRegExp(ChrW($i), '[^a-zA-Z0-9]') = 0) & @LF) ; <------------------------------------  Returns "True"      if tested character is a-z, A-Z, or 0-9
Next

Hopefully you are using SciTE to run this script, as ConsoleWrite() displays in the lower output panel of the SciTE window.

Edit: Added Note about StringIsAlNum() in script example.

Edited by Malkey

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