从字符串中提取子字符串 [英] Extract sub string from a string

查看:222
本文介绍了从字符串中提取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字符串内容( MY_STRING )可以采用以下格式:

The content of my string (MY_STRING)could be in the following format:

bla bla ...这是产品的责任bla bla:#31 5 2 0000 12请确认bla bla ...

或者

bla bla...this is the id of product bla bla: #31 5 2 0000 12, please verify bla bla...

bla bla...this is the id of product bla bla: #31 5 2 0000 12 please verify bla bla...

我想从字符串中提取产品ID。上例中的产品ID为#31 5 2 0000 12

I want to extract out the product ID from the string. The product ID in above example is #31 5 2 0000 12

产品ID的格式是以#开头后跟随机数(长度无限制),数字之间的空格也是任意的

我提取产品ID的当前代码是:

My current code to extract out product ID is:

Pattern pattern = Pattern.compile("^#\\d+(\\s+\\d+)*$");
Matcher matcher = pattern.matcher(MY_STRING);
if(phoneNrMatcher.find()){
    System.out.println(matcher.group(0));                   
}

但它不起作用,有人可以帮助我哪里出错吗?可能正则表达式?

But it does not work, could some one help me where goes wrong? Probably the regular expression?

注意:

- 在我的例子中的内容之前&在ID #31 5 2 0000 12 任意之后。

-In my example the content before & after ID #31 5 2 0000 12 is arbitrary.

- 产品ID字符串始终以#开头,即紧接着没有空格或其他字符的数字

-product ID string always starts with # which is followed by a number immediately without space or other char

推荐答案

试试这个

String test = "bla bla...this is the tag id of product: #31 5 2 0000 12, please verify bla bla...";
// explanation of the Pattern:
//                                |starts with "#"
//                                | |directly followed by digits only
//                                | |   |character class including digits or spaces
//                                | |   |       |ad lib (greedy quantifier)
Pattern pattern = Pattern.compile("#\\d+[\\d\\s]+");

Matcher matcher = pattern.matcher(test);
// using a while group here so you may have multiple matches
while (matcher.find()) {
    System.out.println(matcher.group());
}

输出

#31 5 2 0000 12

解释

在这种情况下,您无需在模式中提及行的开头或结尾。
此外,我的示例中的Pattern允许您在同一个String中找到多个id,前提是它们由既不是空格也不是数字的字符分隔。

You don't need to mention the beginning or end of line in your Pattern in this case. Also, the Pattern in my example would allow you to find more than one id in the same String, provided they are separated by a character that is neither a space nor a digit.

这篇关于从字符串中提取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆