[英]Regex to get String array by splitting with operators ( ! , & , ( , ) , | ) with all string's start and end position In Java
I am new to regex , I want to get string array with all string's start position and end position by splitting operator ( ! , & , ( , ) , | ) . 我是regex的新手,我想通过分割运算符( ! , & , (( , ) , | )来获取包含所有字符串的开始位置和结束位置的字符串数组。
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. 然后使用
matcherobj.start()
和matcherobj.end()
函数找到每个匹配项的开始和结束索引。
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
You can use Strings Split method: 您可以使用Strings Split方法:
String givenString= "!(AString&BString)|CString";
String[] stringArray= givenString.split("[!()&|]");//modify regular expression according to your need
This will give me your array which contains element as you wanted plus other element as ""
. 这将给我您的数组,其中包含所需的元素以及其他作为
""
元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.