user706058
user706058

Reputation: 415

JAVA: replaceAll reg pattern

Considering the following string:

String s = "/static/201105-3805-somerandom/images/optional-folder/filename.gif";

How can I remove the "static/201105-3805-somerandom/" part? The "201105-3805-somerandom" part is completely random but always is composed of: - 6 digits - the "-" char - {1, n} digit chars - the "-" char - {1, n} digit and letter chars

If I use "/static/[0-9]*-[0-9]*-*/";, it replaces everything to the last / instead of the one just after the "{1, n} digit and letter chars", what am I missing?

Upvotes: 2

Views: 2357

Answers (8)

Go Dan
Go Dan

Reputation: 15512

To extend on @John's answer, if the String format should not deviate from the OPs requirement, where "somerandom" is restrained to digit and letter characters, then the following regular expression would work:

"/static/\\d{6}-\\d+-\\p{Alnum}+/"

This assumes the characters are US-ASCII. If, however, you need to support Unicode characters (see Unicode General Category, section 4.5, page 126), you could use the following regular expression:

"/static/\\d{6}-\\d+-(\\p{Lu}|\\p{Ll}|\\p{Nd})+/"

And if "somerandom" changes to be truly random (excluding the / character), the following would work:

"/static/\\d{6}-\\d+-[^/]+/"

Upvotes: 0

wjans
wjans

Reputation: 10115

Try changing it to this:

/static/[0-9]*-[0-9]*-.*?/

* is by default greedy, specifying *? makes it reluctant.

Alternativaly, you could also do this without a regular expression like this:

String s = "/static/201105-3805-somerandom/images/optional-folder/filename.gif";
System.out.println(s.substring(s.indexOf('/', "/static/".length())));

This will start searching for / starting at the index immediately after the static part. It will output:

/images/optional-folder/filename.gif

Upvotes: 4

Petar Ivanov
Petar Ivanov

Reputation: 93090

You need non-greedy *:

"/static/[0-9]*-[0-9]*-.*?/"

Upvotes: 2

Tapas Bose
Tapas Bose

Reputation: 29816

Other than regex, it will work if "/images..." are fixed:

String given = "/static/201105-3805-somerandom/images/optional-folder/filename.gif";
String replaced = given.substring(given.indexOf("/images"), given.length());

Upvotes: 0

franka
franka

Reputation: 1927

Use:

/static/[0-9]{6}-[0-9]*-[a-zA-Z0-9]*//

Your last * before the / simply matched all following characters (including the forward slash), so you need to be more specific and us [a-zA-Z0-9] instead.

Upvotes: 0

kjp
kjp

Reputation: 3116

Try "/static/[0-9]*-[0-9]*-[0-9a-zA-A]*?/" maybe ?

Upvotes: 0

Bohemian
Bohemian

Reputation: 425448

Try this:

    String s = "/static/201105-3805-somerandom/images/optional-folder/filename.gif";
    String regex = "/static/\\d{6}-\\d{4}-.*?/";
    System.out.println(s.replaceAll(regex, "")); // "images/optional-folder/filename.gif"

You where using a "greedy" match .*, but you needed a non-greedy match .*?

Upvotes: 0

Prince John Wesley
Prince John Wesley

Reputation: 63718

s = s.replaceAll("^/static/\\d{6}-\\d{1,}-.*?/","")

Upvotes: 1

Related Questions