Java 用于匹配手机充值代码和Url的正则表达式
我一直在尝试使用Matcher类从一个句子中获取字符串的开始和结束位置。到目前为止,我已经编写了以下正则表达式来查找以下字符串:Java 用于匹配手机充值代码和Url的正则表达式,java,android,regex,Java,Android,Regex,我一直在尝试使用Matcher类从一个句子中获取字符串的开始和结束位置。到目前为止,我已经编写了以下正则表达式来查找以下字符串: ^\*[0-9].*#$ => *1111# ^#[0-9].*[0-9]$ => *1111 ^[0-9].*[0-9]$ => 11111 ^(http:|https:).*[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$
^\*[0-9].*#$ => *1111#
^#[0-9].*[0-9]$ => *1111
^[0-9].*[0-9]$ => 11111
^(http:|https:).*[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$ => http://google.com
你的正则表达式一点也不健全。为什么它不能像预期的那样匹配?在每次交替之前,都要声明开始位置和结束位置。试着这样做:
(\*[0-9]+#|\*[0-9]+(?= )|\+[0-9]+|(?:http:|https:)[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+)
(\*[0-9]+\*[0-9]+(?=)\+[0-9]++(?:http:| https:)[a-zA-Z0-9!@$%^&*(?)+-=\[\]{};:“\\\\,.\/?+)
最后一个正则表达式的有效字符串和无效字符串是什么?最好是平均找到大多数URL。您是否也可以发布代码,以便我们了解您是如何使用Matcher的?@user7854100我的意思是组合的RegEx@PabloEscobar补充说。
SpannableString spanString = new SpannableString(ThatSentence);
Matcher matcher = Pattern.
compile("^.*(\\*[0-9].*#s|#[0-9].*[0-9]|[0-9].*[0-9]|(http:|https:).*[a-zA-Z0-9!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]*).*$")
.matcher(spanString); //combined regex
while (matcher.find())
{
System.out.println("lrkjer hurray found something!");
spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#0000FF"))
, matcher.start(), matcher.end(), 0);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
};
spanString.setSpan(clickableSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
recharge code is *1111# topup code is *1111 customer care number is +18900657765 and find the details at http://blahblah.com/details
(\*[0-9]+#|\*[0-9]+(?= )|\+[0-9]+|(?:http:|https:)[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+)