Solved

Regular expression to allow alpha numeric plus some other characters.

Posted on 2016-09-19
12
39 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 48

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 48

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
 

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 48

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 48

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 62

Accepted Solution

by:
Fernando Soto earned 500 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 48

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 62

Expert Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now