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!

Regular expression pattern matching

Status
Not open for further replies.

akrishmohan

Programmer
May 3, 2002
20
US
I have a string that needs to be matched against a regex and if a match occurs return true/ false.

The variable length string to be compared looks like this
20=34x89,10=23:45,12=10:32, and soon. As you can see, the number and order of strings containing x's and :'s are not known.

I wrote this regex and that doesnt seem to be working

var test = [1-9][0-9]*=[0-9]+x[0-9]+" + "|" + "[1-9][0-9]*=([0-5][0-9]):([0-5][0-9])";

var pattern = test + (,test)*; //only one token could exist.

when i try to use the patten against the string, i get no match found. Whats wrong in the pattern i created. Any help would be appreciated.

Thanks
K

 
First issue... When you are matching a pattern surely you wouldn't use "=" but "=~" to declare you are pattern matching. Or are you just trying to explain what you are trying to do?

Can you give a few examples of a string that would return false and a few that would return true?
 
I was just trying to explain what i was doing. Its just a pseudo code. I know the matching has to be done using =~ in perl.

Some valid ones are:
20=34x45,21=2:45
20=34x45,21=3:01,34=12:32
10=23:12

Invalid ones are
20=34x45,21=2:45,
20
30,23,34
abc

Hope that helps

K
 
chris,

I surmise that he's probably actually coding in javascript.

akrishmohan,

I suggest that you start out by using a simpler regex and then adding to the conditions. Something like the following should work.

Code:
[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [red]/[/red][purple]^#[/purple][red]/[/red][red];[/red] [gray][i]# Skip Comments[/i][/gray]
	[olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [red]/[/red][purple]^[purple][b]\s[/b][/purple]*$[/purple][red]/[/red][red];[/red] [gray][i]# Blank Lines[/i][/gray]
	
	[olive][b]if[/b][/olive] [red]([/red][blue]$_[/blue] =~ [red]m/[/red][purple]^[purple][b]\d[/b][/purple]+=[purple][b]\d[/b][/purple]+[x:][purple][b]\d[/b][/purple]+(,[purple][b]\d[/b][/purple]+=[purple][b]\d[/b][/purple]+[x:][purple][b]\d[/b][/purple]+)*$[/purple][red]/[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Matches: [blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
		[black][b]print[/b][/black] [red]"[/red][purple]Doesn't Match: [blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]# Valid Ones[/teal]
[teal]20=34x45,21=2:45[/teal]
[teal]20=34x45,21=3:01,34=12:32[/teal]
[teal]10=23:12[/teal]

[teal]# Invalid ones are[/teal]
[teal]20=34x45,21=2:45,[/teal]
[teal]20[/teal]
[teal]30,23,34[/teal]
[teal]abc[/teal]

Output:

Code:
>perl scratch.pl
Matches: 20=34x45,21=2:45
Matches: 20=34x45,21=3:01,34=12:32
Matches: 10=23:12
Doesn't Match: 20=34x45,21=2:45,
Doesn't Match: 20
Doesn't Match: 30,23,34
Doesn't Match: abc

While the pattern that you're attempting to make does not look all that complicated, it often helps to start a little smaller. Anyway, hopefully this helps you a little.

- Miller
 
Cool...That was indeed a simple one...I had another question. What if I had a restriction on numbers that appear on either side of : ? So numbers are either side of : should be of the format MM:SS where MM - Minutes and SS - seconds.

Thats why I put in the OR condition in my original post. And somehow it doesnt seem to be working. Not sure where I am making a mistake

So this is the list :

# Valid Ones
20=34x45,21=2:45
20=34x45,21=3:01,34=12:32
10=23:12

# Invalid ones are
20=34x45,21=2:45,
20=34x45,21=100:400 (Invalid because 100 minutes and 400 sec cannot exist) MM should be less than 50 and SS less than 60
30,23,34
abc

I have been breaking my head with it ;)
K
 
The best advice that I can give you is to avoid trying to do things with a single regular expression. Yes, it is possible to build a complex regex to validate practically anything. Yes, it is also fun to learn the intricacies of regex programming to do things that most programmers would praise as clever. However, this is a waste of your time.

Instead, break up your logic into sensible rules. Your string contains a list. Split that list on the comma and validate each substring independently. Then the rules of those substring become a lot more fixed. And even though you might use two different regular expressions to validate since the value could either be a time or a dimension.

It's being stuck with the idea that you must use a single regex that is probably leading to the majority of your difficulties.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top