开发者

Regular Expression Help - String - JWE-766.1.pdf - Pattern - Extract 766

I would like some help with a regular expression.

I have some files like this:

  • JWE-766.1.pdf
  • JWE-766.2.pdf
  • JWE-768.1.pdf
  • JWE-770.1.pdf

I would like a regex pattern to extract the number after 'JWE-'. i.e. 766.

Also, a regex ex开发者_StackOverflowpression to extract 1 and 2 from JWE-766.1.pdf and JWE-766.2.pdf respectively.

Any help would be hugely appreciated.

Thanks guys!


Pattern p = Pattern.compile("^JWE-([0-9]+)\\.([0-9]+)\\.pdf$");
Matcher m = p.matcher("your string here");

if (m.find()) {
    System.out.println(m.group(1)); //first number group
    System.out.println(m.group(2)); //second number group
}

Taken from here

Also, make sure to reuse the Pattern p object if you're looping through a series of strings


JWE-(\d+).(\d+).pdf

should do the trick.

of course when you are creating the string:

Pattern  p = Pattern.compile("JWE-(\\d+)\.(\\d+)\\.pdf");
Matcher m = p.matcher(s); // s contains your filename
if (m.matches()) { 
   String fullName = m.group(0);
   int firstIndex = m.group(1); // 766
   int secondIndex = m.group(2); // 1
}

Have fun


You can use parentheses for capturing groups, and then use Matcher.group(int) to retrieve them after matching.

Try the pattern "^JWE-(\d+)\.(\d?)\.pdf$" and I think group one should be the 766, and group 2 should be 1.

However, as stated above, if the file names are consistent in length, straight manipulation by index will be faster.

...one minute too slow. The Elf King is quick like the wind.


Unless there is more variety to the pattern than this, I would just use substring manipulation in this case.

ie

string s = "JWE-766.1.pdf";
string firstNumber = s.substring( s.indexOf("-" +1), s.indexOf(".") );
string secondNumber = "JWE-766.1.pdf".substring( s.indexOf("." +1), s.lastIndexOf(".") ); 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜