Java 使用regex提取命令后的文本
我有一篇这样的文章Java 使用regex提取命令后的文本,java,regex,Java,Regex,我有一篇这样的文章 Hello. @b this is text 1 #u user1 我想用正则表达式来提取 This is text 1 user1 我的第一次尝试是检测@,#符号,然后将这些文本记录到StringBuilder中。然而,这是一个EditText,用户可以删除一些字符,我不得不从stringbuilder中删除它。最后,它变得越来越复杂,所以我决定找到另一个解决方案 我的问题是:当命令(@b,#u,#b,@u)可用时,如何提取这些文本? 例如: 我的第二次尝试: Pat
Hello. @b this is text 1 #u user1
我想用正则表达式来提取
This is text 1
user1
我的第一次尝试是检测@,#符号,然后将这些文本记录到StringBuilder中。然而,这是一个EditText,用户可以删除一些字符,我不得不从stringbuilder中删除它。最后,它变得越来越复杂,所以我决定找到另一个解决方案
我的问题是:当命令(@b,#u,#b,@u)可用时,如何提取这些文本?
例如:
我的第二次尝试:
Pattern pattern = Pattern.compile("@b (.+) #u (.+)");
Matcher mat = pattern.matcher(charSequence);
while (mat.find()) {
Logger.write("Matcher "+mat.group(1));
}
但它只适用于regex可能是的特定情况@b和#u
Pattern regex = Pattern.compile("(?<=[@#][bu]\\s)(?:(?![@#][bu]).)*");
Pattern regex=Pattern.compile(“(?该regex将是
Pattern regex = Pattern.compile("(?<=[@#][bu]\\s)(?:(?![@#][bu]).)*");
Pattern regex=Pattern.compile(“(?为什么每次用户更改EditText的输入时不应用regexp?是的,我在用户更改输入时使用它。为什么每次用户更改EditText的输入时不应用regexp?是的,我在用户更改输入时使用它。谢谢!我编辑了一点以使其适合我的情况。但你给了我一个很好的解释。模式regex=Pattern.compile(“(?谢谢!我对它进行了一些编辑,以使它适合我的情况。但是你给了我一个很好的解释。Pattern regex=Pattern.compile((?)?
(?<= # Assert that this can be matched before the current position:
[@#] # an "@" or a "#"
[bu] # a "b" or a "u"
\\s # a whitespace character
) # End of lookbehind assertion
(?: # Then match...
(?! # (as long as we're not at the start of the sequence
[@#] # "@" or "#"
[bu] # and "b" or "u"
) # End of lookahead)
. # any character
)* # any number of times