Regex find first 5-7 occurrences of a set of digits within a string
Using these strings as an example:
Code:
<a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=145148&playTogether=True',960,540,943437);return false;" title=""> Code:
145148 Code:
145148 943437 Code:
\d{5,7} |
Give this a try:
Code:
sed 's/.*=\([0-9]\{5,7\}\).*/\1/' infile |
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 |
Quote:
The solution I gave does use a regexp: [0-9]{5,7} -> any number, 5 to 7 times. |
Many thanks Druuna,
I modified my original post to reflect what I need but a quick summary: Sample Strings Code:
<a onclick="doShowCHys=1;ShowWindowN(0,'/daman/man.php?asv4=145148&playTogether=True',960,540,943437);return false;" title=""> Code:
145148 Code:
145148 943437 Code:
\d{5,7} |
Sorry, I probably don't get it.
Code:
$ cat foobar |
Quote:
Is there a communication failure? Daniel B. Martin |
Or maybe grep:
Code:
grep -oP '(?<==)\d{5,7}' file |
Quote:
Daniel B. Martin |
Sorry,I should have added that I am using Pythons and the "re" module. When I attempt to use the regex expression:
Code:
.*=\([0-9]\{5,7\}\).*/\1/ [url]http://gskinner.com/RegExr/[url] Try it. |
Sorry,I should have added that I am using Python and the "re" module. When I attempt to use the regex expression:
Code:
.*=\([0-9]\{5,7\}\).*/\1/ Code:
[0-9]{5,7} http://gskinner.com/RegExr/ Try it. |
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: Code:
(?<==)\d{5,7} |
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: Code:
(?<==)\d{5,7} |
Awesome
Code:
(?<==)\d{5,7} |
[QUOTE=grail;4906864]
Quote:
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 Code:
123456 |
All times are GMT -5. The time now is 03:19 AM. |