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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP! How to create this RegExp... 1

Status
Not open for further replies.

Markh51

Programmer
May 20, 2003
81
GB
Hi,

I am having trouble trying format my Expression to match my form fields to.

What I need is:
Any amount of LETTERS (upper or lower)
and if entered:
only one space and "-" at a time.

This is what I have so far but without any luck:

[a-zA-Z]+[ \-]{0,1}

Many Thanks,
Mark
 
can you give an example of a text string that you would want to produce a match
 
OK,

Example:

These Should match:-

MYTEST
MyTest
my test
MY TEST
my-test
My-Test

However, these should NOT:
my--test
my---test
my test
my test

Only allow upper and lower case letters (any amount), but when used only one space and "-" at a time.

Cheers.
 
Sorted! I've tried this and it works...

$text="

MYTEST
MyTest
my test
MY TEST
my-test
My-Test

my--test
my---test
my test
my test";

while ($text =~ m/([a-zA-Z]+[- A-Z]{1,1}[a-zA-Z]+)/g) {

print "matched: $1\n"

}


Difficult one because the [- A-Z] has to be followed by a {1,1} or it can be ignored and will pick up all of the lines.

Basically it matches at least one character [a-zA-Z]...
then at least one match of a hyphen, space or upper-case char ]+[- A-Z]{1,1}...
then it matches at least one character again [a-zA-Z]...

Really hope this sorts it - let me know how you get on!

- and hopefully i'll get one of those nice stars!!!

Duncan
 
Why when I use the above patter can I enter:

<ANYTEXT> ????

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top