Reputation: 67
Code:
https://aaa.bbb.net/ccc/211099_589944494365122_1446403980_n.jpg
How can I get 589944494365122
out of that string using regex?
The best I can do so far is _(.*)
resulting 589944494365122_1446403980_n.jpg
Upvotes: 0
Views: 143
Reputation: 71019
I would go with \d+_(\d+)_\d+_n\.jpg
, but depending on the exact specification of the URL this may need a little bit of tweaking.
Also depending on the language, this may need to be altered a little bit. The solution I suggest will work for instance in Ruby (as well as many other regex implementations). Here \d
matches any digit and \d+
means one or more digits. I assume the letter before .jpg
is always n
but you may change this by either replacing n
with .
(any character) or with \w
(any word character).
Upvotes: 1
Reputation: 11586
This works;
var s = "https://aaa.bbb.net/ccc/211099_589944494365122_1446403980_n.jpg";
var m = /_([^_]*)/.exec(s);
console.log( m[1] ); // 589944494365122
Upvotes: 1
Reputation: 195289
The rule of extraction I can see in your input is:
211099_589944494365122_1446403980
[0-9]+_ part we want _[0-9]+
so a regex with look-behind and look-ahead will help:
'(?<=\d_)\d+(?=_\d)'
test with grep:
kent$ echo " https://aaa.bbb.net/ccc/211099_589944494365122_1446403980_n.jpg"|grep -Po '(?<=\d_)\d+(?=_\d)'
589944494365122
Upvotes: 1
Reputation: 21517
First, you should generalize your problem description, like that: How can I get the longest non-empty substring of digits after the first _
in string? The regexp you literally asked for is (589944494365122)
, but that's not what you expect.
According to my guess about what you want, the answer could be _(\d+)
.
Upvotes: 2