Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解析并捕获字符串末尾的数字_Java_String - Fatal编程技术网

Java 解析并捕获字符串末尾的数字

Java 解析并捕获字符串末尾的数字,java,string,Java,String,我正在构建一个程序来遍历日志文件,其中包含如下条目: en halo%20reach%20noble%20actual%20in%20theater 1 659 en Wazir_Khan_Mosque 2 77859 en Waziristan_War 3 285976 en Wazirpur_Upazila 1 364 我想输出出现在每个字符串末尾的数字(即659、77859、285976、285976、364)。正如您所看到的,这些数字的位数不同 如何从这些字符串中提取最后的数字?一种可

我正在构建一个程序来遍历日志文件,其中包含如下条目:

en halo%20reach%20noble%20actual%20in%20theater 1 659
en Wazir_Khan_Mosque 2 77859
en Waziristan_War 3 285976
en Wazirpur_Upazila 1 364
我想输出出现在每个字符串末尾的数字(即659、77859、285976、285976、364)。正如您所看到的,这些数字的位数不同


如何从这些字符串中提取最后的数字?

一种可能的解决方案是根据空格来提取字符串:

String[] splitted = myStr.split("\\s+");
然后以最后一个元素为例:

splitted[splitted.length - 1];
如果要
int
值,应使用


另一种解决方案是使用and..

一种可能的解决方案是根据空格对字符串进行排序:

String[] splitted = myStr.split("\\s+");
然后以最后一个元素为例:

splitted[splitted.length - 1];
如果要
int
值,应使用


另一种解决方案是使用and..

读取每一行并分配给这样的字符串

String line = "en halo%20reach%20noble%20actual%20in%20theater 1 659";
那么这样做应该会给你最后一个数字

String words[] = line.split("\\s");
System.out.println(words[words.length - 1]);

如果您正在读取每一行并分配给这样的字符串

String line = "en halo%20reach%20noble%20actual%20in%20theater 1 659";
那么这样做应该会给你最后一个数字

String words[] = line.split("\\s");
System.out.println(words[words.length - 1]);

我通常不推荐正则表达式,因为它们在Stackoverflow上经常被滥用(特别是在涉及XML/HTML时),但这是学习如何使用它们的最佳案例

在空格上进行拆分,虽然这样做不会像这种方法那样有效;如果空格变化,它将继续工作,并允许您在一个操作中捕获所有其他数据,您可能最终需要:

:单击以了解其工作原理的说明

然后使用它:

final Pattern p = Pattern.compile("^en\\s+(.*)\\s+(\\d+)\\s+(\\d+)$");
final Matcher m = p.matches("en Wazirpur_Upazila 1 364");
final String g1 = m.group(1); // Wazirpur_Upazila
final String g2 = m.group(2); // 1
final String g3 = m.group(3); // 364

我通常不推荐正则表达式,因为它们在Stackoverflow上经常被滥用(特别是在涉及XML/HTML时),但这是学习如何使用它们的最佳案例

在空格上进行拆分,虽然这样做不会像这种方法那样有效;如果空格变化,它将继续工作,并允许您在一个操作中捕获所有其他数据,您可能最终需要:

:单击以了解其工作原理的说明

然后使用它:

final Pattern p = Pattern.compile("^en\\s+(.*)\\s+(\\d+)\\s+(\\d+)$");
final Matcher m = p.matches("en Wazirpur_Upazila 1 364");
final String g1 = m.group(1); // Wazirpur_Upazila
final String g2 = m.group(2); // 1
final String g3 = m.group(3); // 364

试试这个,可能不是很好

public static void main(String[] args) throws FileNotFoundException, IOException {
    BufferedReader br = new BufferedReader(new FileReader("log.txt"));
try {
    String line = br.readLine();
    List<String> stringList = new ArrayList<>();
    while(line!=null) {
        String[] strsplit = line.split(" ");
        line = br.readLine();
        for(int i=3;i<strsplit.length;i+=4) {
            stringList.add(strsplit[i]);
        }
    }
    System.out.println(stringList);
} finally {
    br.close();
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException、IOException{
BufferedReader br=新的BufferedReader(新文件读取器(“log.txt”);
试一试{
String line=br.readLine();
List stringList=新建ArrayList();
while(行!=null){
字符串[]strsplit=line.split(“”);
line=br.readLine();

对于(inti=3;i试试这个,可能不是很好

public static void main(String[] args) throws FileNotFoundException, IOException {
    BufferedReader br = new BufferedReader(new FileReader("log.txt"));
try {
    String line = br.readLine();
    List<String> stringList = new ArrayList<>();
    while(line!=null) {
        String[] strsplit = line.split(" ");
        line = br.readLine();
        for(int i=3;i<strsplit.length;i+=4) {
            stringList.add(strsplit[i]);
        }
    }
    System.out.println(stringList);
} finally {
    br.close();
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException、IOException{
BufferedReader br=新的BufferedReader(新文件读取器(“log.txt”);
试一试{
String line=br.readLine();
List stringList=新建ArrayList();
while(行!=null){
字符串[]strsplit=line.split(“”);
line=br.readLine();

对于(int i=3;我正在写相同的,添加
Integer.parseInt()
来完成你的答案,如果他想使用整数;)正在写相同的,添加
Integer.parseInt()
来完成你的答案,如果他想使用整数;)