Re: Extract numbers from in between parentheses with regex
Brain, Thanks for the direction! I really need to learn more about regex. I will certainly read those! Kristofer Original Message Follows From: Brian <[EMAIL PROTECTED]> Reply-To: [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Re: Extract numbers from in between parentheses with regex Date: Wed, 26 Jun 2002 12:21:32 -0500 Kristofer, you might want to check out the man page's perlrequick and perlre- I found them pretty useful when I was first learning regex (the man page perlretut isn't bad either). Either way, try this; my $string = '(608)-555-1234'; $string =~ /\((\d+)\)-(\d+)-(\d+)/; this results in $1=608, $2=555, $3=1234; For extracting a match in a regex, you just enclose it in parenthesises... perl dumps it then to var's $[1-9]. One thing to consider is if a match isn't found- unless I'm mistaken, perl won't do anything to the $[1-9] match variables leaving the variables with there previous values. Something to watch/test for... ~Brian On Wednesday 26 June 2002 11:21, Kristofer Hoch wrote: > Hi all, > Please forgive the simple nature of this question. I have never really > used regular expression extensivly. > > Here goes. I am trying to extract a number from in between two > parenthesis. I want the first value I find (from right to left) in a > string. These numbers could be phone number area codes, or comments. > > Could someone please help, so that I can shamelessly use it all over the > place? > > Thank you > Kristofer. > > > > > > _ > Send and receive Hotmail on your mobile device: http://mobile.msn.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] _ Send and receive Hotmail on your mobile device: http://mobile.msn.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Extract numbers from in between parentheses with regex
Kristofer, you might want to check out the man page's perlrequick and perlre- I found them pretty useful when I was first learning regex (the man page perlretut isn't bad either). Either way, try this; my $string = '(608)-555-1234'; $string =~ /\((\d+)\)-(\d+)-(\d+)/; this results in $1=608, $2=555, $3=1234; For extracting a match in a regex, you just enclose it in parenthesises... perl dumps it then to var's $[1-9]. One thing to consider is if a match isn't found- unless I'm mistaken, perl won't do anything to the $[1-9] match variables leaving the variables with there previous values. Something to watch/test for... ~Brian On Wednesday 26 June 2002 11:21, Kristofer Hoch wrote: > Hi all, > Please forgive the simple nature of this question. I have never really > used regular expression extensivly. > > Here goes. I am trying to extract a number from in between two > parenthesis. I want the first value I find (from right to left) in a > string. These numbers could be phone number area codes, or comments. > > Could someone please help, so that I can shamelessly use it all over the > place? > > Thank you > Kristofer. > > > > > > _ > Send and receive Hotmail on your mobile device: http://mobile.msn.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Extract numbers from in between parentheses with regex
Kristofer -- ...and then Kristofer Hoch said... % % David(s), % Thank you for your help. It works perfectly now. I am adding in both of % y'alls names into my class file for your help. Happy to help! % % % (803 is South Carolina, which should explain the accent on the yall) I know. Less than a week and I'm home in Atlanta! Yee ha! % % % Thank you botha again. % Kristofer. HAND :-D -- David T-G * It's easier to fight for one's principles (play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie (work) [EMAIL PROTECTED] http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg! msg05596/pgp0.pgp Description: PGP signature
Re: Extract numbers from in between parentheses with regex
David(s), Thank you for your help. It works perfectly now. I am adding in both of y'alls names into my class file for your help. (803 is South Carolina, which should explain the accent on the yall) Thank you botha again. Kristofer. Original Message Follows From: David T-G <[EMAIL PROTECTED]> To: perl beginners cgi <[EMAIL PROTECTED]> CC: Kristofer Hoch <[EMAIL PROTECTED]> Subject: Re: Extract numbers from in between parentheses with regex Date: Wed, 26 Jun 2002 14:03:52 -0500 Kristofer -- and then Kristofer Hoch said... % % David, % Thank you very much for your help. Don't know Utah, I have a lot of % friends from there. The expression you provided is almost what I am after.. % Here is my string. % % my $String = "Characters(803), Value(3)"; % % What I am trying to get is "803", but I keep getting "(803)". Is there a % way to get 803 without the enclosing parens? His method almost works for you; you simply have to change around the parens: [zero] [1:55pm] ~> \ perl -e 'my $var = "Characters(803), Value(3)"; $var =~ /\((\d+)\)/; \ my $output = $1 ; print "output is $output\n";' output is 803 When doing string matching, as you've found, () will match a pattern and then store the result as $1 or $2 or whatever is appropriate. David's first example explodes a bit to ( \(numbers\) ) which means "start saving your place for $1 later" and "match an actual opening paren and then some digits and an actual closing paren" and then, finally, "you're finished saving your place for $1". Of course, you want the in-the-text parens outside the save-your-place expression, so it changes from (\(\d+\)) to \((\d+)\) and if we knew that your target digits would be the first on the line like in the example instead of "My2Thing(803), Value(3)" which would mess us up, we could just use (\d+\) because it would simply match the digits and ignore the parens completely. % % Thank you % Kristofer HTH & HAND :-D -- David T-G * It's easier to fight for one's principles (play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie (work) [EMAIL PROTECTED] http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg! << attach3 >> _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Extract numbers from in between parentheses with regex
Kristofer -- ...and then Kristofer Hoch said... % % David, % Thank you very much for your help. Don't know Utah, I have a lot of % friends from there. The expression you provided is almost what I am after.. % Here is my string. % % my $String = "Characters(803), Value(3)"; % % What I am trying to get is "803", but I keep getting "(803)". Is there a % way to get 803 without the enclosing parens? His method almost works for you; you simply have to change around the parens: [zero] [1:55pm] ~> \ perl -e 'my $var = "Characters(803), Value(3)"; $var =~ /\((\d+)\)/; \ my $output = $1 ; print "output is $output\n";' output is 803 When doing string matching, as you've found, () will match a pattern and then store the result as $1 or $2 or whatever is appropriate. David's first example explodes a bit to ( \(numbers\) ) which means "start saving your place for $1 later" and "match an actual opening paren and then some digits and an actual closing paren" and then, finally, "you're finished saving your place for $1". Of course, you want the in-the-text parens outside the save-your-place expression, so it changes from (\(\d+\)) to \((\d+)\) and if we knew that your target digits would be the first on the line like in the example instead of "My2Thing(803), Value(3)" which would mess us up, we could just use (\d+\) because it would simply match the digits and ignore the parens completely. % % Thank you % Kristofer HTH & HAND :-D -- David T-G * It's easier to fight for one's principles (play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie (work) [EMAIL PROTECTED] http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg! msg05594/pgp0.pgp Description: PGP signature
Re: Extract numbers from in between parentheses with regex
David, Thank you very much for your help. Don't know Utah, I have a lot of friends from there. The expression you provided is almost what I am after. Here is my string. my $String = "Characters(803), Value(3)"; What I am trying to get is "803", but I keep getting "(803)". Is there a way to get 803 without the enclosing parens? Thank you Kristofer Original Message Follows From: <[EMAIL PROTECTED]> To: "Kristofer Hoch" <[EMAIL PROTECTED]>, <[EMAIL PROTECTED]> Subject: Re: Extract numbers from in between parentheses with regex Date: Wed, 26 Jun 2002 11:26:23 -0600 -- my $var = "(801) 555-"; # ya, ok so I'm in utah :) $var =~ /(\(\d+\))/; # real paren, escape paren, \d = digits, # + = one or more, escape paren, real paren my $area_code = $1; # set to what came from between the real parens -- but if you are looking to match anything between the parens, you probably want something more like this: -- my $var = "some text (my comment) and more text"; $var =~ /(\([^\(\)]+\))/; # real paren, escape paren, [^\(\)] = any non ( or ), # + = one or more, escape paren, real paren my $matched_value = $1; # set to what came from between the real parens -- Regards, David - Original Message - From: "Kristofer Hoch" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, June 26, 2002 10:21 AM Subject: Extract numbers from in between parentheses with regex Hi all, Please forgive the simple nature of this question. I have never really used regular expression extensivly. Here goes. I am trying to extract a number from in between two parenthesis. I want the first value I find (from right to left) in a string. These numbers could be phone number area codes, or comments. Could someone please help, so that I can shamelessly use it all over the place? Thank you Kristofer. _ Send and receive Hotmail on your mobile device: http://mobile.msn.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] _ Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Extract numbers from in between parentheses with regex
-- my $var = "(801) 555-"; # ya, ok so I'm in utah :) $var =~ /(\(\d+\))/; # real paren, escape paren, \d = digits, # + = one or more, escape paren, real paren my $area_code = $1; # set to what came from between the real parens -- but if you are looking to match anything between the parens, you probably want something more like this: -- my $var = "some text (my comment) and more text"; $var =~ /(\([^\(\)]+\))/; # real paren, escape paren, [^\(\)] = any non ( or ), # + = one or more, escape paren, real paren my $matched_value = $1; # set to what came from between the real parens -- Regards, David - Original Message - From: "Kristofer Hoch" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, June 26, 2002 10:21 AM Subject: Extract numbers from in between parentheses with regex Hi all, Please forgive the simple nature of this question. I have never really used regular expression extensivly. Here goes. I am trying to extract a number from in between two parenthesis. I want the first value I find (from right to left) in a string. These numbers could be phone number area codes, or comments. Could someone please help, so that I can shamelessly use it all over the place? Thank you Kristofer. _ Send and receive Hotmail on your mobile device: http://mobile.msn.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Extract numbers from in between parentheses with regex
Hi all, Please forgive the simple nature of this question. I have never really used regular expression extensivly. Here goes. I am trying to extract a number from in between two parenthesis. I want the first value I find (from right to left) in a string. These numbers could be phone number area codes, or comments. Could someone please help, so that I can shamelessly use it all over the place? Thank you Kristofer. _ Send and receive Hotmail on your mobile device: http://mobile.msn.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]