Solved

Simple regex

Posted on 2011-02-24
14
419 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
  • 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 74

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 74

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now