Shpigford
Shpigford

Reputation: 25348

Select a certain part of this string

I need to select a small portion of a string.

Here's an example string: http://itunes.apple.com/app/eyelashes/id564783832?uo=5

I need: 564783832

A couple of things to keep in mind:

This will ultimately be implemented with Ruby.

Upvotes: 0

Views: 89

Answers (4)

KekuSemau
KekuSemau

Reputation: 6856

Here's mine:

[\w\/]+id(\d+)(\?|$)

Upvotes: 0

Boris the Spider
Boris the Spider

Reputation: 61148

You can match some sequence of digits preceded by "id" - this assumes that those are the only sequence of digits preceded by "id":

(?<=id)\d++

A test case in Java:

public static void main(String[] args) {
    String input = "http://itunes.apple.com/app/eyelashes/id564783832?uo=5";
    Pattern pattern = Pattern.compile("(?<=id)\\d++");
    Matcher matcher = pattern.matcher(input);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }
}

Output

564783832

Upvotes: 0

Kent
Kent

Reputation: 195059

without knowing your language/tool, just assume look behind was supported.

'(?<=id)\d+'

Upvotes: 2

Zombo
Zombo

Reputation: 1

With awk

awk '{print $2}' FS='(id|?)'

Upvotes: 0

Related Questions