Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regular expression to allow alpha numeric plus some other characters.

Posted on 2016-09-19
12
Medium Priority
?
80 Views
Last Modified: 2016-09-19
Please can I have a regular expression for C# to allow alpha numeric characters plus the following:
( ) & = . / , : ' + -

I tried this which seems to work unless the forbidden character is last.  I based myself on this site.
https://www.owasp.org/index.php/OWASP_Validation_Regex_Repository

Function ValidateRtrDescription(ByVal Description As String) As Boolean

        Dim emailRegex As New System.Text.RegularExpressions.Regex("[^[a-zA-Z0-9 ()&=./,:'+-]$")  '%[A-Za-z,~:/;#-.]%'
        Dim emailMatch As System.Text.RegularExpressions.Match = emailRegex.Match(Description)
        Return emailMatch.Success
    End Function
0
Comment
Question by:AlHal2
  • 5
  • 5
  • 2
12 Comments
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 41804546
HI,

pls try
       Dim emailRegex As New System.Text.RegularExpressions.Regex("[^[a-zA-Z0-9\(\)&=\./,:'\+-]$")  '%[A-Za-z,~:/;#-.]%'

Open in new window

REgards
0
 

Author Comment

by:AlHal2
ID: 41804563
It let this through.  ~ is not valid.

Hong Kong Stock Exchange Equities+~
0
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 41804600
I've based my answer with line 2

then try

       Dim emailRegex As New System.Text.RegularExpressions.Regex("[^[a-zA-Z0-9\(\)&=\./,;:'\~#-]$")  '%[A-Za-z,~:/;#-.]%'

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AlHal2
ID: 41804626
I'm using this expression which you gave me.  It's rejecting these which should be accepted.

Hong Kong Stock Exchange Equities+:
Hong Kong Stock Exchange Equities
HongKongStockExchangeEquities+;

Dim emailRegex As New System.Text.RegularExpressions.Regex("[^[a-zA-Z0-9\(\)&=\./,;:'\~#-]$")  '%[A-Za-z,~:/;#-.]%'

Open in new window

0
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 41804636
Sorry I've deleted the space and the plus
       Dim emailRegex As New System.Text.RegularExpressions.Regex("[^[a-zA-Z0-9\(\)&=\s\./,;:'#~\+-]$")  '%[A-Za-z,~:/;#-.]%'

Open in new window

0
 

Author Comment

by:AlHal2
ID: 41804786
It's rejecting this.

Hong Kong Stock Exchange Equities
0
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 41804801
let's try
       Dim emailRegex As New System.Text.RegularExpressions.Regex("^[a-zA-Z0-9\(\)&=\s\./,;:'#~\+-]+$")  '%[A-Za-z,~:/;#-.]%'

Open in new window

0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 41804838
Hi AlHal2;

Try this pattern to see if it helps.
Dim emailRegex As New System.Text.RegularExpressions.Regex("^[a-zA-Z0-9 \(\)&=\./,:;'\+-]+$")

Open in new window

0
 

Author Comment

by:AlHal2
ID: 41804854
It accepted
Hong Kong Stock Exchange Equities+~
even though ~ is forbidden.
0
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 41804859
then try
       Dim emailRegex As New System.Text.RegularExpressions.Regex("^[a-zA-Z0-9\(\)&=\s\./,;:'#\+-]+$")

Open in new window

0
 

Author Closing Comment

by:AlHal2
ID: 41804861
Please ignore what I said before.  This works so far.

Thanks,
Alistair
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41805298
Not a problem Alistair, glad to help.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question