2

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?

1
  • 1
    if it is always 3 slashes, you don't need a regex. just use the indexOf-method. Commented Jul 1, 2011 at 9:38

8 Answers 8

4

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
Sign up to request clarification or add additional context in comments.

Comments

2

You need non-greedy *:

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

Comments

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

Comments

0

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 .*?

Comments

0

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

Comments

0

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.

Comments

0

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());

Comments

0

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+-[^/]+/"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.