Solved

Simple regex

Posted on 2011-02-24
14
432 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:Abiel de Groot
[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
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34973086
^([A-Za-z0-9\-\_]+)$

Open in new window

0
 
LVL 5

Author Comment

by:Abiel de Groot
ID: 34973245
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
 
LVL 21

Expert Comment

by:silemone
ID: 34973282
try

^[A-Za-z0-9\-\_](\.[A-Za-z]*)*
0
Industry Leaders: 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!

 
LVL 21

Expert Comment

by:silemone
ID: 34973303
oops

^[A-Za-z0-9\-\_]+([.][A-Za-z]*)*
0
 
LVL 21

Expert Comment

by:silemone
ID: 34973318
Tested this one...this should work.


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

Expert Comment

by:silemone
ID: 34973337
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
 
LVL 5

Author Comment

by:Abiel de Groot
ID: 34973504
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
 
LVL 21

Expert Comment

by:silemone
ID: 34973817
^[A-Za-z0-9\-\_]+
but no $ sign at the end
0
 
LVL 21

Expert Comment

by:silemone
ID: 34973874
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34975490
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
 
LVL 5

Accepted Solution

by:
Abiel de Groot earned 0 total points
ID: 34977562
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34979080
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
 
LVL 5

Author Comment

by:Abiel de Groot
ID: 34979272
Point taken, kaufmed.

A.
0
 
LVL 5

Author Closing Comment

by:Abiel de Groot
ID: 35015361
This worked best
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

635 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