正则表达式通过与运营商的分裂得到String数组(!,&安培;,(,),|)与所有字符串的开始和结束位置在Java [英] Regex to get String array by splitting with operators ( ! , & , ( , ) , | ) with all string's start and end position In Java
本文介绍了正则表达式通过与运营商的分裂得到String数组(!,&安培;,(,),|)与所有字符串的开始和结束位置在Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的正则表达式,我想要得到的字符串数组通过拆分操作符( &放所有的字符串的开始位置和结束位置;! ( ) | )
例如:
1。 !(ASTRING&安培; BString)|的CString
0123456789输出应该是字符串数组与所有的字符串的起始位置和结束位置:
[ASTRING,BString,CString的...]
每一个字符串的位置:
ASTRING(7长度)= GT; 2到8个
BString => 10至16
CString的= GT; 19至25
2.(ASTRING)及!(BString)|(CString的)
3.(ASTRING | BString)及!CString的
!4.(ASTRING及((BString | CString的))| DString!)
解决方案
而不是分裂,你可以做配套。 [^()及|!] +
任意字符,但不相匹配(
或 )
或 |
或
或&安培;!
的一个或更多次。然后发现使用 matcherobj.start()
和 matcherobj.end()
功能每场比赛的开始和结束索引。
字符串S1 =(ASTRING&安培; BString)|!CString的;
匹配器M = Pattern.compile。([!^()及|] +)匹配(S1);
而(m.find())
{的System.out.println(m.group()+=>中+ m.start()+至+(m.end() - 1));}
输出:
ASTRING => 2到8个
BString => 10至16
CString的= GT; 19至25
I am new to regex , I want to get string array with all string's start position and end position by splitting operator ( ! , & , ( , ) , | ) .
Examples:
1. !(AString&BString)|CString
0123456789
Output should be String Array with all string's start position and end position:
[AString,BString,CString...]
Every String Position:
AString (7 length) => 2 to 8
BString => 10 to 16
CString => 19 to 25
2. (AString)&(BString)|!(CString)
3. !(AString|BString)&CString
4. !(AString&(!(BString|CString))|DString)
解决方案
Instead of splitting, you could do matching. [^()!&|]+
matches any character but not of (
or )
or |
or !
or &
one or more times. Then find the start and end index of each match using matcherobj.start()
and matcherobj.end()
functions.
String s1 = "!(AString&BString)|CString";
Matcher m = Pattern.compile("[^()!&|]+").matcher(s1);
while(m.find())
{
System.out.println(m.group() + " => " + m.start() + " to " + (m.end()-1));
}
Output:
AString => 2 to 8
BString => 10 to 16
CString => 19 to 25
这篇关于正则表达式通过与运营商的分裂得到String数组(!,&安培;,(,),|)与所有字符串的开始和结束位置在Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文