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

Help with regular expression

Status
Not open for further replies.

GregArtemides

Programmer
May 23, 2003
12
CY
if (!ereg("([0-9]{9})\(xml)|(txt))","123457890.txt")) echo "error!";

What's wrong with this regular expression? I need one that will check a filename has 9 numeric digits and a .txt or .xml extension.
The one above should echo the error since it's 10 digits long before the dot, but it doesn't work.
Where did I go wrong?
 
Your regular expression is invalid. Try this one:
[tt]if (!ereg("([0-9]{9})\.(xml|txt)","123457890.txt")) echo "error!";[/tt]

//Daniel
 
You're really close there, but you need to bind that regular expression to the front and end of the string. The problem is that a regular expression like that will find any string which has nine numbers followed by a .txt|.xml, it doesn't care what comes before the nine numbers or what comes after the .txt or .xml

That leaves you with
Code:
if (!ereg("^([0-9]{9})\.(xml|txt)","1245789320.txt")) echo "error!";

-Rob
 
Shoot I got it half right, I mean
Code:
if (!ereg("^([0-9]{9})\.(xml|txt)$","1245789320.txt")) echo "error!";
-Rob
 
Thanks skiflyer, your solutions works! So I was forgetting the ^ and $, but when I put them it worked just fine. And thanks danilhozac for trying.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top