Solved

Regular expression to allow alpha numeric plus some other characters.

Posted on 2016-09-19
12
65 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
12 Comments
 
LVL 52

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 52

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 52

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 52

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 63

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 52

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 63

Expert Comment

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

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

623 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