Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx help needed to make checkValid() - thank you!

Status
Not open for further replies.

ScaryEders

Programmer
Dec 26, 2004
10
GB
I'm wanting to create a perl sub called "checkValid". When I use this sub, I want to be able to pass to two string values to it and for it use perl's RegEx abilities to return a true or false response. However, I'm not too knowledgeable when it comes to RegEx, which is why I'm asking for your help in making my sub.

The two strings to be passed to checkValid() will be in the form:

checkValid($variableToCheck, 'typeToCheck')

'typeToCheck' could be the value 'email', 'url', 'filename', 'username', or 'password'. The function of checkValid() will be to return whether the $variableToCheck is a valid form of the ‘typeToCheck’.

I want to define each of the ‘types’ as follows:

email: (letters, numbers, fullstops, and underscores only)(the symbol ‘@’)(letters, at least one fullstop and numbers only)

url: (letters, numbers, at least one fullstop, underscores, dollar signs, forward slashes, hyphens, wiggly hyphens, plus signs, exclamation marks, asterisks, brackets and commas only)

filename: (letters and numbers only)

username: (letters, numbers and underscores only)

password: (letters, numbers and spaces only)

Any help in creating my sub would be much appreciated!

- Ed Jones
 
Email regex:
Code:
/^[\w\d\._]+@[\w\d]+\.[\w\d]+$/
Url regex (new suggestion):
Code:
/^[http:\/\/]?[\w\d\-]+\.[\w\d\-]\.[\w][\/[\w\d_\$\-~\+!\*\[\],\.]+]*$
Filename regex:
Code:
/^[\w\d]*$/
Username regex:
Code:
/^[\w\d_]*$/
Password regex:
Code:
/^[\d\w\s]*$/

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Comments on the re's above:

1. \w means "letters, digits, and underscores." \d and _ are subsets of \w. Using \d or _ in the same character class as \w is redundant. [\w\d_], for example, is the equivalent of \w, no char class necessary.

Letters only: [a-zA-Z]
Digits only: [0-9]
Letters and digits only: [a-zA-Z0-9]
Letters, digits, and underscores: [a-zA-Z0-9_] or \w

2. From perlretut:
The special characters for a character class are "-]\^$".
A dot is not special inside a character class and so doesn't need to be backslashed.

3. The string "http:" inside a char class does not match the string "http:". It matches any of the individual chars "h", "t", "p", and ":", in any order.

4. The last 3 regexps will all match an empty string because of the * quantifier. I think you want to use + instead.


 
Just giving a starting point (and showing my inexperience) ... thanks mikevh.

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Another try for URL checking.

/^(http:\/\/)?([-\w]+\.)+[A-Za-z]+(\/~?[-\w.]+[-\]\[\w\$!,+.#*]*)*\/?$/
[tt]
good
good
bad
good
bad
[].org bad
good
how.cz good
how.c-z bad
how.999 bad
how-to bad
good
how.org/~bob/ good
www.how.org/~bob[foo] good
good
good
bad
www.how.org/bob/[bar] bad
bad
good
bad
good
good
[/tt]
 
I played with some link validation before and threw up a little FAQ on it. That's as "good" as I could feasibly think to make it. Yes, it is just a regex, so it can be condensed to a single huge and ugly one, but it truly is hideous.

Many people use the link detecting abilities of HTML::FromText.

________________________________________
Andrew

I work for a gift card company!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top