简体   繁体   English

如何在Java中使用正则表达式在特定位置查找文本中的数字

[英]How to find a number in text at specific location using regex in java

How to create a method which will find a number in String Text. 如何创建在字符串文本中查找数字的方法。 I contain List of Strings which contain text like: 我包含字符串列表,其中包含类似以下内容的文本:

Radius of Circle is 7 cm
Rectangle 8 Height is 10 cm
Rectangle Width is 100 cm, Some text

Now I have to find all the number in these lines which are coming before cm so that I don't mistakenly find any other number. 现在,我必须找到cm之前的这些行中的所有数字,以便不会错误地找到其他任何数字。

How can it happen 怎么会发生

A matching regular expression would be: 匹配的正则表达式为:

(\d+) cm

In order to obtain the captured number before the cm , you can use the Pattern and Matcher classes: 为了获取cm之前的捕获数字,可以使用PatternMatcher类:

String line = "Radius of Circle is 7 cm";
Pattern pattern = Pattern.compile("(\\d+) cm");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
    System.out.println("Value: " + matcher.group(1));
}

This example only matches the line from example (1), but can be easily repeated for each the lines contained in your list. 本示例仅与示例(1)中的行匹配,但可以轻松地对列表中包含的每一行重复进行操作。 See Java Regex Capture Groups for more information. 有关更多信息,请参见Java Regex Capture Groups

You have to find groups with only digits in the string using the following regex: 您必须使用以下正则表达式查找字符串中仅包含数字的组:

(?:\d{1,})
  • \\d{1,} matches a digit (equal to [0-9]) \\ d {1,}匹配一个数字(等于[0-9])
  • {1,} Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed {1,}量词-匹配一次和无限次,尽可能多的次数,并根据需要返回
  • (?:) The capturing group (?:)捕获组

Main##: 主要##:

import java.util.regex.Pattern;  
import java.util.Scanner;  
import java.util.regex.Matcher;    
public class RegexExample{    
    public static void main(String[] args){    
        Scanner sc=new Scanner(System.in);  
        while (true) {    
            Pattern pattern = Pattern.compile("(?:\\d{1,})");    
            System.out.println("Enter text:");  
            Matcher matcher = pattern.matcher(sc.nextLine());    
            boolean found = false;    
            while (matcher.find()) {    
                System.out.println("I found the text "+matcher.group()+" starting at index "+    
                 matcher.start()+" and ending at index "+matcher.end());    
                found = true;    
            }    
            if(!found){    
                System.out.println("No match found.");    
            }    
        }    
    }    
} 

Example: 例:

Enter text:
Radius of Circle is 7 cm
I found the text 7 starting at index 20 and ending at index 21
Enter text:
Rectangle 8 Height is 10 cm
I found the text 8 starting at index 10 and ending at index 11
I found the text 10 starting at index 22 and ending at index 24
Enter text:
Rectangle Width is 100 cm, Some text
I found the text 100 starting at index 19 and ending at index 22
Enter text:

Note: In java code, the character \\ it's an escape character. 注意:在Java代码中,字符\\是转义字符。 That's why you have to append another \\ . 这就是为什么您必须附加另一个\\

The correct pattern to use here is: 此处使用的正确模式是:

(\\d+)\\s+cm\\b

For a one liner, we can try using String#replaceAll : 对于一个班轮,我们可以尝试使用String#replaceAll

String input = "Rectangle Width is 100 cm, Some text";
String output = input.replaceAll(".*?(\\d+)\\s+cm\\b.*", "$1");
System.out.println(output);

Or, to find all matches in a given text, we can try using a formal pattern matcher: 或者,要查找给定文本中的所有匹配项,我们可以尝试使用正式的模式匹配器:

String input = "Rectangle Width is 100 cm, Some text";
String pattern = "(\\d+)\\s+cm\\b";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
while (m.find()) {
    System.out.println("Found measurement: " + m.group(1));
}

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

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