简体   繁体   English

子串可删除第一个句号之前和第二个句号之后的所有内容

[英]Substring to remove everything before first period and after second

So I have a filename that looks like this: 所以我有一个像这样的文件名:

myFile.12345.txt myFile.12345.txt

If I wanted to end up with just the "12345" how would I go about removing that from the filename if the 12345 could be anywhere between 1 and 5 numbers in length? 如果我只想以“ 12345”结尾,那么如果12345的长度可以在1到5个数字之间,该如何从文件名中删除呢?

If you are sure that there would be 2 periods . 如果您确定会有两个期间. for sure 当然

String fileName = string.split("\\.")[1]

Assuming you want to extract all the numbers, you could use a simple regex to remove all the non-digits characters: 假设您要提取所有数字,则可以使用简单的正则表达式删除所有非数字字符:

String s = "myFile.12345.txt";
String numbers = s.replaceAll("[^\\d]","");
System.out.println(numbers); //12345

Note: It would not work with file12.12345.txt for example 注意:例如,它file12.12345.txt用于file12.12345.txt

static final Pattern P = Pattern.compile("^(.*?)\\.(.*?)\\.(.*?)$");
...
...
...
Matcher m = P.matcher(input);
if (m.matches()) {
  //String first = m.group(1);
  String middle = m.group(2);
  //String last = m.group(3);
  ...
}

you can use this 你可以用这个

String s="ghgj.7657676.jklj";
String p = s.substring(s.indexOf(".")+1,s.lastIndexOf("."));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM