开发者

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/201开发者_运维百科105-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?


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


You need non-greedy *:

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


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


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


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


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜