Simple regex

Hi All,

I have been looking for a simple regex that only allows the following:

A-Z,a-z,09,-,_

It must allow a hyphen, underscore but no spaces.

Many thanks.

A.
LVL 5
Abiel de GrootDeveloperAsked:
Who is Participating?
 
Abiel de GrootConnect With a Mentor DeveloperAuthor Commented:
I should really take some time to understand these things... I played around with some and got this to work.

"[^0-9a-zA-Z_-]"

I call it like this.

strPageURL = regExFileName.Replace(Request.Form("txtFileName"),"")

It basically need to validate a string which a user enters into a text field. I want it to strip any none allowed chars.


A.
0
 
sjklein42Commented:
^([A-Za-z0-9\-\_]+)$

Open in new window

0
 
Abiel de GrootDeveloperAuthor Commented:
Hmm....

This works

strPageURL = pageOp23.asp

But If I remove the .asp, I get nothing.

I am collecting it like this in classic asp.

strPageURL = regExFileName.Replace(Request.Form("txtFileName"),"")
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
silemoneCommented:
try

^[A-Za-z0-9\-\_](\.[A-Za-z]*)*
0
 
silemoneCommented:
oops

^[A-Za-z0-9\-\_]+([.][A-Za-z]*)*
0
 
silemoneCommented:
Tested this one...this should work.


^[A-Za-z0-9\-\_]+([.][A-Za-z]*){1}$
0
 
silemoneCommented:
sorry...

so explanation

^[A-Za-z0-9\-\_]+([.][A-Za-z]*){0,1}$

works for

anystring containing:  asdfadfadf
                                     dadfadfad.sdds
                                   adfasdfasd.asp  

the  (.asp) can only occur once or zero times

is this what you're looking for?
0
 
Abiel de GrootDeveloperAuthor Commented:
How about removing the dot... no dots. I will strip the '.asp' from the string before it gets parsed by the regex.

I want anything stripped from the string if it is not

A-Z,a-z,09,_,-   (Allow underscore and hyphen)

I really appreciate your help on this

many thanks

A.
0
 
silemoneCommented:
^[A-Za-z0-9\-\_]+
but no $ sign at the end
0
 
silemoneCommented:
I think you may be looking for substitution
s/^([A-Za-z0-9\-\_]+)*([.][A-Za-z]*){0,1}$)/$1/

this looks for a string like before, but removes '.asdfsd' portion
0
 
käµfm³d 👽Commented:
There is no apparent reason why sjklein42's solution shouldn't work for what you've described. There would seem to be something different about your data than what you shown. In actuality, having "pageOp23.asp" as the source data should not have matched whereas "pageOp23" should have matched.
0
 
käµfm³d 👽Commented:
You would probably have gotten you answer sooner if you had clearly defined your requirement initially. You initially said, "that only allows the following." You then changed the requirement to, "I want it to strip any none allowed chars." Those are two different operations. In the future, be sure to clearly identify what it is you are trying to accomplish.

Glad you got your answer  = )
0
 
Abiel de GrootDeveloperAuthor Commented:
Point taken, kaufmed.

A.
0
 
Abiel de GrootDeveloperAuthor Commented:
This worked best
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.