Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于解析引用参数的Java正则表达式_Java_Regex_Arguments_Pattern Matching_Command Line Arguments - Fatal编程技术网

用于解析引用参数的Java正则表达式

用于解析引用参数的Java正则表达式,java,regex,arguments,pattern-matching,command-line-arguments,Java,Regex,Arguments,Pattern Matching,Command Line Arguments,以下场景需要Java正则表达式模式: 案例1: 输入字符串: "a" "aA Bb" cCc 123 4 5 6 7xy "\"z9" "\"z9$^" "a b c "a b" "c "a b" p q r "x y z" "a b" p q r "x \"y \"z\"" 匹配项: a a b aA Bb cCc 123 4 5 6 7xy "z9 "z9$^ a b p q r x y z a b p q r x y "z" a b p q r x "y "

以下场景需要Java正则表达式模式:

案例1:

输入字符串:

"a"
"aA Bb" cCc 123 4 5 6 7xy "\"z9" "\"z9$^"
"a b c
"a b" "c
"a b" p q r "x y z"
"a b" p q r "x \"y \"z\""
匹配项:

a
a b
aA Bb
cCc
123
4
5
6
7xy
"z9
"z9$^
a b
p 
q 
r
x y z
a b
p 
q
r
x y "z"
a b
p 
q 
r
x "y "z"
a
b
案例2:

输入字符串:

"a b"
a b
匹配项:

a
a b
aA Bb
cCc
123
4
5
6
7xy
"z9
"z9$^
a b
p 
q 
r
x y z
a b
p 
q
r
x y "z"
a b
p 
q 
r
x "y "z"
a
b
案例3:

输入字符串:

"a"
"aA Bb" cCc 123 4 5 6 7xy "\"z9" "\"z9$^"
"a b c
"a b" "c
"a b" p q r "x y z"
"a b" p q r "x \"y \"z\""
匹配项:

a
a b
aA Bb
cCc
123
4
5
6
7xy
"z9
"z9$^
a b
p 
q 
r
x y z
a b
p 
q
r
x y "z"
a b
p 
q 
r
x "y "z"
a
b
案例4:

输入字符串:

"a"
"aA Bb" cCc 123 4 5 6 7xy "\"z9" "\"z9$^"
"a b c
"a b" "c
"a b" p q r "x y z"
"a b" p q r "x \"y \"z\""
匹配项:

None - since the quotes are unbalanced, hence pattern match fails.
None - since the quotes are unbalanced, hence pattern match fails.
案例5:

输入字符串:

"a"
"aA Bb" cCc 123 4 5 6 7xy "\"z9" "\"z9$^"
"a b c
"a b" "c
"a b" p q r "x y z"
"a b" p q r "x \"y \"z\""
匹配项:

None - since the quotes are unbalanced, hence pattern match fails.
None - since the quotes are unbalanced, hence pattern match fails.
案例6:

输入字符串:

"a"
"aA Bb" cCc 123 4 5 6 7xy "\"z9" "\"z9$^"
"a b c
"a b" "c
"a b" p q r "x y z"
"a b" p q r "x \"y \"z\""
匹配项:

a
a b
aA Bb
cCc
123
4
5
6
7xy
"z9
"z9$^
a b
p 
q 
r
x y z
a b
p 
q
r
x y "z"
a b
p 
q 
r
x "y "z"
a
b
案例7:

输入字符串:

"a b" p q r "x y \"z\""
匹配项:

a
a b
aA Bb
cCc
123
4
5
6
7xy
"z9
"z9$^
a b
p 
q 
r
x y z
a b
p 
q
r
x y "z"
a b
p 
q 
r
x "y "z"
a
b
案例8:

输入字符串:

"a"
"aA Bb" cCc 123 4 5 6 7xy "\"z9" "\"z9$^"
"a b c
"a b" "c
"a b" p q r "x y z"
"a b" p q r "x \"y \"z\""
匹配项:

a
a b
aA Bb
cCc
123
4
5
6
7xy
"z9
"z9$^
a b
p 
q 
r
x y z
a b
p 
q
r
x y "z"
a b
p 
q 
r
x "y "z"
a
b
当然,最简单的是:

案例9:

输入字符串:

"a"
"aA Bb" cCc 123 4 5 6 7xy "\"z9" "\"z9$^"
"a b c
"a b" "c
"a b" p q r "x y z"
"a b" p q r "x \"y \"z\""
匹配项:

a
a b
aA Bb
cCc
123
4
5
6
7xy
"z9
"z9$^
a b
p 
q 
r
x y z
a b
p 
q
r
x y "z"
a b
p 
q 
r
x "y "z"
a
b
尝试使用一种模式,但它似乎不符合上述所有情况

public List<String> parseArgs(String argStr) {
    List<String> params = new ArrayList<String>();
    String pattern = "\\s*(\"[^\"]+\"|[^\\s\"]+)";
    Pattern quotedParamPattern = Pattern.compile(pattern);
    Matcher matcher = quotedParamPattern.matcher(argStr);
    while (matcher.find()) {
        String param = matcher.group();
            System.out.println(param);
            params.add(param);
    }
    return params;
}

public void test(String argStr) {
    String[] testStrings = new String[]{"a", "a b", "a b \"c\"", "a b \"c"};
    for(String s: testStrings){
        parseArgs(s);
    }
}
公共列表解析参数(字符串argStr){
List params=new ArrayList();
字符串模式=“\\s*(\”[^\“]+\“\[^\\s\“]+)”;
Pattern quotedParamPattern=Pattern.compile(Pattern);
Matcher Matcher=quotedParamPattern.Matcher(argStr);
while(matcher.find()){
String param=matcher.group();
系统输出打印项次(参数);
参数添加(参数);
}
返回参数;
}
公共无效测试(字符串argStr){
String[]testStrings=新字符串[]{“a”、“AB”、“AB\“c\”、“AB\“c”};
for(字符串s:testStrings){
parseArgs(s);
}
}
尝试一下:

(“\S+?(?:\S+?)*”\S+?)

在行动中看到它:


只需运行全局匹配并返回
\1
。返回的每个捕获组都应该包含您想要的内容。

要开始,您可以使用以下基于Java正则表达式的代码:

public List<String> parseArgs(String argStr, Pattern validPattern, Pattern parsePattern) {
    List<String> params = null;
    if (validPattern.matcher(argStr).matches()) {
        params = new ArrayList<String>();
        Matcher matcher = parsePattern.matcher(argStr); 
        while (matcher.find())
            params.add( matcher.group(1) != null ? matcher.group(1) : matcher.group(2));
    }
    return params;
}

public void parseIt() {
    Pattern validatePattern = Pattern.compile("^(?=(?:(?:[^\"]*\"){2})*[^\"]*$).*$");
    Pattern parsingPattern = Pattern.compile("\"([^\"]*)\"|(\\w+)");

    String[] testStrings = new String[]{
             "a", "a b", "a b \"c\"", "a b \"c", "\"a b\" p q r \"x y z\""};
    for(String s: testStrings) {
        List<String> parsedList = parseArgs(s, validatePattern, parsingPattern);
        System.out.printf("input: %-30s :: parsed: %s%n", s, parsedList);
    }
}

PS:虽然我注意到您最近的编辑中也添加了嵌套引号,但这个答案需要改进。

我不知道使用正则表达式解决问题的直接方法

String[] testStrings = new String[]{
         "a", "a b", "a b \"c\"", "a b \"c", "\"a b\" p q r \"x y z\""};
Pattern parsingPattern = Pattern.compile("(\".*?\")|( [^ ^\"]+)");
for(String s: testStrings) {
   s=s.replace("(?<!\\)\\"","@@@");
}
for(String s: testStrings) {
    List<String> params = null;
    int count = StringUtils.countMatches(s, "\"");
    if(count%2==0){
    params = new ArrayList<String>();
    Matcher matcher = parsePattern.matcher(s); 
    while (matcher.find())
        params.add( matcher.group(1) != null ? matcher.group(1) : matcher.group(2));
   }
}
但是你可以用一些唯一的关键字替换内部转义序列,然后你可以用正则表达式匹配你的字符串

String[] testStrings = new String[]{
         "a", "a b", "a b \"c\"", "a b \"c", "\"a b\" p q r \"x y z\""};
Pattern parsingPattern = Pattern.compile("(\".*?\")|( [^ ^\"]+)");
for(String s: testStrings) {
   s=s.replace("(?<!\\)\\"","@@@");
}
for(String s: testStrings) {
    List<String> params = null;
    int count = StringUtils.countMatches(s, "\"");
    if(count%2==0){
    params = new ArrayList<String>();
    Matcher matcher = parsePattern.matcher(s); 
    while (matcher.find())
        params.add( matcher.group(1) != null ? matcher.group(1) : matcher.group(2));
   }
}
String[]testStrings=新字符串[]{
“a”、“ab”、“ab\'c\”、“ab\'c\”、“ab\'pqr\'xyz\”;
Pattern parsingPattern=Pattern.compile(“(\”*?\”)|([^^\“]+)”;
for(字符串s:testStrings){
s=s.replace((?已经编写了一个类“CLIParser”,它将给出结果

//instantiate the CLIParser 

CLIParser parser = new CLIParser("\"a b\" p q r \"x y z\"");

//call the method getTokens which gives you the result.

ArrayList<String> resultTokens = parser.getTokens();


###################CLI Parser Class definition#################################

class CLIParser {
    private String cmdString;

    public CLIParser(String cmdString) {
        this.cmdString = cmdString;
    }

    public ArrayList<String> getTokens() throws Exception {
        ArrayList<String> finalTokens = new ArrayList<String>();
        ArrayList<StringBuffer> tokens = new ArrayList<StringBuffer>();
    char inArray[] = this.cmdString.toCharArray();
    StringBuffer token = new StringBuffer();
    int valid = checkIfTheStringIsValid(inArray);
    if (valid == -1) {
        for (int i = 0; i <= inArray.length; i++) {

            if (i != inArray.length) {
                if ((inArray[i] != ' ') && (inArray[i] != '"')) {
                    token.append(inArray[i]);
                }

                if ((inArray[i] == '"') && (inArray[i - 1] != '\\')) {
                    i = i + 1;
                    while (checkIfLastQuote(inArray, i)) {
                        token.append(inArray[i]);
                        i++;
                    }
                }
            }
            if (i == inArray.length) {
                tokens.add(token);
                token = new StringBuffer();
            } else if (inArray[i] == ' ' && inArray[i] != '"') {
                tokens.add(token);
                token = new StringBuffer();
            }
        }
    } else {
        throw new InvalidCommandException(
                "Invalid command. Couldn't identify sequence at position "
                        + valid);
    }
    for(StringBuffer tok:tokens){
        finalTokens.add(tok.toString());
    }
    return finalTokens;
}

private static int checkIfTheStringIsValid(char[] inArray) {
    Stack myStack = new Stack<Character>();
    int pos = 0;
    for (int i = 0; i < inArray.length; i++) {
        if (inArray[i] == '"' && inArray[i - 1] != '\\') {
            pos = i;
            if (myStack.isEmpty())
                myStack.push(inArray[i]);
            else
                myStack.pop();
        }
    }
    if (myStack.isEmpty())
        return -1;
    else
        return pos;
}

private static boolean checkIfLastQuote(char inArray[], int i) {
    if (inArray[i] == '"') {
        if (inArray[i - 1] == '\\') {
            return true;
        } else
            return false;
    } else
        return true;
}
}
//实例化CLIParser
CLIParser parser=新的CLIParser(“\'ab\'pqr\'xyz\”);
//调用给出结果的getTokens方法。
ArrayList resultTokens=parser.getTokens();
###################CLI解析器类定义#################################
类语法分析器{
私有字符串;
公共CLIParser(字符串cmdString){
this.cmdString=cmdString;
}
public ArrayList getTokens()引发异常{
ArrayList finalTokens=新的ArrayList();
ArrayList标记=新的ArrayList();
char inArray[]=this.cmdString.toCharArray();
StringBuffer令牌=新的StringBuffer();
int valid=checkIfTheStringIsValid(inArray);
如果(有效==-1){

对于(int i=0;i),它是可以解决的,但您需要展示解决它的一些努力。至少将所有示例输入字符串放在字符串数组中(
String[]
)然后将Java代码放在这里。@anubhava添加了Java代码和一些输入字符串。我猜您的实际字符串将不仅仅是单个小写字母。它们是否需要包含大写字母、数字、特殊字符等?它们的长度是否有限制?因为可能有大写字母、数字、特殊字符等字符等,但没有限制。我不确定你想用它做什么,但我确信答案是在你达到这个目的之前。是否有Java正则表达式字符串可用,因为上面的模式需要转义斜杠和引号,即使转义,它似乎也不适用于所有情况。尝试了这个输入字符串:“a b”p q r“x y”z”这似乎适用于大多数情况。但我正在尝试提取包含转义引号的字符串。尝试使用以下命令:“aA Bb”cCc 123 4 5 6 7xy“\“z9”“\“z9$^”这应该给我“z9作为第8个参数和”z9$^作为第9个。谢谢。是的,我做了一个记录,因为我没有注意到你之前的问题中的转义/嵌套引号。什么正则表达式可以匹配上述所有情况?实际上转义没有结束。甚至反斜杠也可以转义。你是如何获得此输入的?是的,这是正确的。这就是我正在开发的应用程序的工作方式ilding。即使这是有效的“aA Bb”cCc 123 4 5 6 7xy“\”\\z9“\”z9$^”。并且字符串正在从stdin读取。这似乎是一个公平的解决方案。谢谢!非常感谢您的帮助:)