[SOLVED] Regex find first 5-7 occurrences of a set of digits within a string
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Using a regular expression, how can I extract just the first 5-7 digits of a string(anywhere in the string)and end there? So in this case I only want to print out only the "first" set of 5-7 digits of a string which would give me an output of:
Code:
145148
1451486
1451489
45148
and "not" several sets on the same string
Code:
145148 943437
45148 94343
I tried:
Code:
\d{5,7}
and it grabs every occurrence on the same line??
Last edited by metallica1973; 03-07-2013 at 11:12 AM.
I apologize, I meant using a regular expression so,using a regex expression, how can I extract just the first 5-7 digits of a string(anywhere in the string)and end there? So in this case I only want to print out only the "first" set of 5-7 digits of a string which would give me an output of:
Code:
145148
1451486
1451489
45148
Last edited by metallica1973; 03-07-2013 at 10:02 AM.
I apologize, I mean using a regular expression so,using regex espression, how can I extract just the first 5-7 digits of a string(anywhere in the string)and end there? So in this case I only want to print out only the "first" set of 5-7 digits of a string which would give me an output of:
Please give an appropriate example.
The solution I gave does use a regexp: [0-9]{5,7} -> any number, 5 to 7 times.
Hey Daniel ... Pretty simple except for the look behind part really.
(?<==) - This says to look in front of the rest of the matching regex and look for an equals (=) sign, but as we are only looking for it, it will not be included in the final output
The other newish part may also be the -P option for grep which is to use Perl type regular expressions.
To OP ... using the site you provided, the follow works just fine:
Hey Daniel ... Pretty simple except for the look behind part really.
(?<==) - This says to look in front of the rest of the matching regex and look for an equals (=) sign, but as we are only looking for it, it will not be included in the final output
The other newish part may also be the -P option for grep which is to use Perl type regular expressions.
To OP ... using the site you provided, the follow works just fine:
(?<==) - This says to look in front of the rest of the matching regex and look for an equals (=) sign ...[/code]
Thank you for this explanation. Now I see that your solution works for the sample file provided by the OP.
I interpret the problem statement this way:
Extract the first numeric string in each line which is of length 5, 6, or 7.
(No reliance on an equals sign.)
If possible, modify your solution to handle this InFile ...
Code:
this is9the way44the world123456ends35
not 54321 with a444444bang 42 but a9whimper
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.