简体   繁体   English

括号和逗号的正则表达式组合

[英]Regex combination for parenthesis and commas

I need help with forming the regex for reading a file following the structure:我需要帮助形成正则表达式以读取结构后的文件:

2050206,"Abella, Jan Vincent P",BSECE,1,Male

How can I ignore the comma inside the double parenthesis?如何忽略双括号内的逗号? Thank you in advance先感谢您

Here is an example of how to split on a comma by passing a regex to split .这是一个如何通过将正则表达式传递给split来拆分逗号的示例。

The negative lookahead (?![^"]*"[^"]*$) prevents a comma being matched if there is only one " ahead in the line.如果该行中只有一个" ,则否定前瞻(?![^"]*"[^"]*$)可防止匹配逗号。

(?m) turns on MULTILINE mode so that $ matches the end of a line and not the end of the whole string. (?m)打开MULTILINE模式,以便$匹配一行的结尾而不是整个字符串的结尾。

public class Example {
    public static void main(String[] args) {
        String string = 
            "2050206,\"Abella, Jan Vincent P\",BSECE,1,Male\n" +
            "2050207,\"Theron, Charlize\",BSECE,2,Female";
        String regex = "(?m),(?![^\"]*\"[^\"]*$)";
        for (String s : string.split(regex)) {
            System.out.println(s);
        }
    }
}

Prints:印刷:

2050206
"Abella, Jan Vincent P"
BSECE
1
Male
2050207
"Theron, Charlize"
BSECE
2
Female

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM