Link to home
Start Free TrialLog in
Avatar of mike99c
mike99c

asked on

Regular expression to accept alpha numeric, underscore and accented characters

I have the following ASP/VBScript function that returns true if the given string contains alpha numeric characters or an underscore.

Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^\w*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window


I would like to extend the regular expression so it also allows accented characters such as áéíóúÁÉÍÓÚ etc.

Currently this function returns false if I pass the following value:
abcáéíóú

What I do know is that I would like to allow the unicode range u00C8 to u00D6 but I do not know how to add this to the current regular expression.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Try it this way:
Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^[\w\xC8-\xD6]*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window

Avatar of mike99c
mike99c

ASKER

Hi kaufmed,

I just tested your solution and it does not work as it returns false for accented characters.

I have however modifed the script with a new regular expression to add the unicode range mentioned and it now works.

Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^\w[u00C0-\u00FC]*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mike99c

ASKER

Need to partially accept the solution from kaufmed.
Avatar of mike99c

ASKER

Solution was assisted from a previous one submitted by kaufmed so will partially accept.
Avatar of mike99c

ASKER

I had researched the previous question and it was my mistake not to have added the link in my original question. I thought I would simplify the whole thing by giving a clue as to what I wanted to add rather than confuse the issue by linking to another solution. I will know better next time.
@Qlemo

Excuse me sir, but I was not intending to pander for points. I was merely making a comment that I thought he had already asked something similar. Had I been pandering, I would have selected "Object" vs. "Post."

As point of fact, I recalled answering the question I linked to in the post above, but I couldn't find it at the time. This is why the solution I posted was inaccurate. I just happened to find the previous solution after I searched for both mine and mike99c's handles.

I am fine with the original resolution of the asker in closing the question without delegating points as my solution was inaccurate. I do ask the courtesy of not being accused of something that I don't feel I gave any hint of doing.

Thank you.
Vielen Dank.
Avatar of mike99c

ASKER

For closure, I would like to correct the script as follows:

 
Function StringIsAlphaNumeric(TheString)
Dim re, IsAlphaNumeric

    Set re = New RegExp
    re.Pattern = "^[\w\u00C0-\u00FC]*$"
    IsAlphaNumeric = re.Test(TheString)
    Set re = Nothing
    
    StringIsAlphaNumeric = IsAlphaNumeric

End Function

Open in new window