简体   繁体   中英

regex not accepting white space

Below is a code snippet :

    String reFundId = "(\\s{0,})<fund_id>([a-zA-Z0-9]{1,8}\\s)</fund_id>(\\s\\S0,})";
    Pattern patFundId = Pattern.compile(reFundId, Pattern.CASE_INSENSITIVE);
    String text = "<fund_id>AB11 </fund_id>";
    String fundVar = parseFundId(text,patFundId);

public static String parseFundId(String text,Pattern patFundId)
    {
        String fund_id = null;
        Matcher mat = patFundId.matcher("" + text);
        if (mat.find())
        {
            String s = mat.group(2);
            fund_id = (s != null ? s.trim() : null);
        }
        return fund_id;
    }

Regex pattern is not accepting <fund_id>AB11 </fund_id> . What changes are required in regex pattern?

这样就可以了,它还可以与多个空格一起使用:

String reFundId = "(\\s*)<fund_id>([a-zA-Z0-9]{1,8}\\s*)</fund_id>(\\s*)";

You have unescaped forward slash in </fund_id> . Just replace it with <\\\\/fund_id> . Also you lack square brackets at the end. Replace \\\\s\\\\S0,} with [\\\\s\\\\S]* .

You have a typo in your regex. Change this:

"(\\s{0,})<fund_id>([a-zA-Z0-9]{1,8}\\s)</fund_id>(\\s\\S0,})";

To

"(\\s{0,})<fund_id>([a-zA-Z0-9]{1,8}\\s)</fund_id>(\\s\\S{0,})";
                                               missing --^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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