나는 정규식이 있습니다. [\\.|\\;|\\?|\\!][\\s]
문자열을 분할하는 데 사용됩니다. 그러나 따옴표로 묶는다면 나는 그것을 나누기를 원하지 않습니다. . ; ? !
.RegEx 따옴표 사이의 텍스트를 무시하려면
2
A
답변
6
나는 나누기 대신 패턴 & 일치를 사용하고 싶습니다.
데모 : 생산
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String text = "start. \"in quotes!\"; foo? \"more \\\" words\"; bar";
String simpleToken = "[^.;?!\\s\"]+";
String quotedToken =
"(?x) # enable inline comments and ignore white spaces in the regex \n" +
"\" # match a double quote \n" +
"( # open group 1 \n" +
" \\\\. # match a backslash followed by any char (other than line breaks) \n" +
" | # OR \n" +
" [^\\\\\r\n\"] # any character other than a backslash, line breaks or double quote \n" +
") # close group 1 \n" +
"* # repeat group 1 zero or more times \n" +
"\" # match a double quote \n";
String regex = quotedToken + "|" + simpleToken;
Matcher m = Pattern.compile(regex).matcher(text);
while(m.find()) {
System.out.println("> " + m.group());
}
}
}
: 당신이 볼 수 있듯이
> start
> "in quotes!"
> foo
> "more \" words"
> bar
, 그것은 또한 인용 토큰 내부 탈출 따옴표를 처리 할 수 있습니다.
0
여기 일치 항목에서 따옴표를 무시하기 위해 내가하는 일이 있습니다.
(?:[^\"\']|(?:\".*?\")|(?:\'.*?\'))*? # <-- append the query you wanted to search for - don't use something greedy like .* in the rest of your regex.
는
(?:[^\"\']|(?:\".*?\")|(?:\'.*?\'))*?[.;?!]\s*
을 할 수있는, 당신의 정규식이를 적응 난 당신이 * 구문 분석 *하지 정규식 분할에 대해 생각하기 시작해야한다고 생각. 어떤 예제 입력으로도 쉽게 대답 할 수 있습니다. – deceze
구문 분석은 옵션이지만 RegEx에서 수행하는 방법을 알고 싶습니다. 이 일을하는 방법이 있습니까? –
RegEx는 구문 분석을 대신 할 수 없기 때문에 사용할 용도에 관계없이 RegEx는이를 위해 사용할 도구가 아닙니다. 나는이 퀘스트를 포기하고 위에서 언급 한 파싱에 대해 배워야한다고 생각합니다. –