[英]Regex Extract number between multiple words combination
Need to extract mobile numbers based on multiple words(LandLine|Mobile) scan from the below input.需要从下面的输入中提取基于多个单词(LandLine|Mobile)扫描的手机号码。 I am not able to extract all the 3 numbers.
我无法提取所有 3 个数字。 Need to read the number before and after the given words combination .Please assist
需要阅读给定单词组合前后的数字。请协助
Words: (LandLine|Mobile)文字:(座机|手机)
String line = "i'm Joe my LandLine number is 987654321, another number 123456789 is my Mobile and wife Mobile number is 776655881";
String pattern = "(Mobile|LandLine)([^\\d]*)(\\d{9})|" //Forward read
+"(\\d{9})([^\\d]*)(Mobile|LandLine)"; //Backward read
Pattern r = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = r.matcher(line);
while(matcher.find()) {
System.out.println(line.substring(matcher.start(), matcher.end()));
}
Code Output:
LandLine number is 987654321
123456789 is my Mobile and wife Mobile
Expected Output:
LandLine number is 987654321
123456789 is my Mobile
Mobile number is 776655881
The pattern "(LandLine|Mobile)\\\\D*\\\\d{9}|\\\\d{9}.*?(LandLine|Mobile)"
seems to fit the bill:模式
"(LandLine|Mobile)\\\\D*\\\\d{9}|\\\\d{9}.*?(LandLine|Mobile)"
似乎符合要求:
import java.util.Arrays;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
class Main {
public static void main(String[] args) {
var line = "i'm Joe my LandLine number is 987654321, another number 123456789 is my Mobile and wife Mobile number is 776655881";
var pattern = "(LandLine|Mobile)\\D*\\d{9}|\\d{9}.*?(LandLine|Mobile)";
var res = Pattern
.compile(pattern)
.matcher(line)
.results()
.map(MatchResult::group)
.toArray(String[]::new);
System.out.println(Arrays.toString(res));
}
}
Output:输出:
[LandLine number is 987654321, 123456789 is my Mobile, Mobile number is 776655881]
This adds a lazy quantifier ?
这增加了一个懒惰的量词
?
to .*?
.*?
along with some minor semantic optimizations like \\\\D
instead of [^\\\\d]
.以及一些小的语义优化,如
\\\\D
而不是[^\\\\d]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.