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

Regular expression question

Status
Not open for further replies.

akrishmohan

Programmer
May 3, 2002
20
US
Hi

I am trying to determine the regular exp for finding a comma separated integer pattern with in a string

e.g
Hello++1,2,3++World

There could be just integer (ofcourse without any comma at the end) or many integers (comma separated)>

Let me know.

Thanks
K
 
Does is always start/end with +++ or is that just an example?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Just to simplify things I would want to match only Comma Separated Integers. I want to match just one integer (with no comma) and I DONT want to match something like this 1,2,3,

This is the summary

1,2,3,..n - ok
1,2,3,...n, - NOT OK
1 - ok

Hope this is clear

Thanks
K
 
Kinda.. do you have have any real data you can show us?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Below is the right data

12,43,45 - MATCH
12,34,56,77 - MATCH
112,23, - NO MATCH
11,3,45,66,77,88, - NO MATCH
90 - MaTCH
39, - NO MATCH

38,,45,9 - NO MATCH

Thx
K
 
Your request is not yet very clear:
-do you just want to determine whether your string is acceptable (answer:true-false)
-do you want to also extract the integers when the string is OK?
-taking your last request, are you sure that the strings contain only figures and commas (and no plus or minus I suppose, what about end of line?)
Assuming yes-no-yes this is a possible regexp (the string being in [tt]$_[/tt]):
[tt]!/,,|,$/;[/tt]

Franco
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 

I'm not sure if I understood you correctly....
if you file "myfile.txt" looks like this
Code:
Hello++12,43,45++World
Hello++12,34,56,77++World
Hello++112,23,++World
Hello++11,3,45,66,77,88,++World
Hello++90++World
Hello++39,++World
Hello++38,,45,9++World
then maybe this is what you need
Code:
open (FILE,"myfile.txt");
while (<FILE>) {
    $foo = $_;
    if ( $foo =~ /(\d{1,},?([^,]\d{1,}|,)*)/ ) {
        unless  ( $1 =~ /,$/ ) {
            print "$1\n";
        }
    }
}
close FILE;
this will print
Code:
12,43,45
12,34,56,77
90


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Lets forget about the presence of Hello World in the string.

I have a request string that I need to validate on the server. The request string (ideally) should be comma separated positive integers. If the string is NOT properly formatted, I reject the string. So yes, its a true or false situation.

For reference below is a list of valid strings :

12,43,45 - MATCH - return true
12,34,56,77 - MATCH - return true
112,23, - NO MATCH - return false
11,3,45,66,77,88, - NO MATCH return false
90 - MaTCH return true
39, - NO MATCH return false

38,,45,9 - NO MATCH return false

Hope this helps
Thanks
K
 
Code:
while( <DATA> ) {
	print /^\d+(,\d+)*$/ ? "MATCH " : "NO MATCH ";
	print;
}

__DATA__
12,43,45
12,34,56,77
112,23,
11,3,45,66,77,88,
90
39,
38,,45,9
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top