java - 如何将特殊字符包含到字符串数组中。同时使用正则表达式模式的特殊字符拆分字符串

标签 java regex

String s = "select from(select * LEAF_CATEG_ID,a.FROM_EMAIL_ADDRESS,a.HARD_BOUNCE_TYPE";
    Pattern p=Pattern.compile("[^a-zA-Z0-9]",Pattern.CASE_INSENSITIVE);
    String[] parts=p.split(s);

    for(int i=0;i<parts.length;i++){
        System.out.println("After finding special characters i am able to split the characters:"+parts[i]);

    }

我的期望输出是:

After finding special characters i am able to split the characters:select
After finding special characters i am able to split the characters:from
After finding special characters i am able to split the characters:(
After finding special characters i am able to split the characters:select
After finding special characters i am able to split the characters:*
After finding special characters i am able to split the characters:
After finding special characters i am able to split the characters:LEAF
After finding special characters i am able to split the characters:_
After finding special characters i am able to split the characters:CATEG
After finding special characters i am able to split the characters:_
After finding special characters i am able to split the characters:ID
After finding special characters i am able to split the characters:,
After finding special characters i am able to split the characters:a
After finding special characters i am able to split the characters:.
After finding special characters i am able to split the characters:FROM

But what I get is:

After finding special characters i am able to split the characters:select
After finding special characters i am able to split the characters:from
After finding special characters i am able to split the characters:select
After finding special characters i am able to split the characters:
After finding special characters i am able to split the characters:
After finding special characters i am able to split the characters:LEAF
After finding special characters i am able to split the characters:CATEG
After finding special characters i am able to split the characters:ID
After finding special characters i am able to split the characters:a
After finding special characters i am able to split the characters:FROM

I want to split the above string into String array of object including the special characters but my current code is not including the special character,its skipping the special character Please help me out,Thanks in advance....

最佳答案

要在 Java 中拆分和保留分隔符,您需要使用lookarounds:前瞻和后视与您选择的分隔符的组合。

在您的案例中,分隔符除 ASCII 字母或数字以外的任何字符。可以用

来实现
Pattern p=Pattern.compile("(?<=[^a-z0-9])|(?=[^a-z0-9])",Pattern.CASE_INSENSITIVE);

请注意,您不需要 [A-Z]因为你正在使用 CASE_INSENSITIVE旗帜。

参见 IDEONE demo这是the regex demo .

正则表达式匹配两个空位置

  • (?<=[^a-z0-9]) - 以 a-z 以外的字符开头, A-Z0-9
  • (?=[^a-z0-9]) - 后跟除 a-z 以外的字符, A-Z0-9 .

关于java - 如何将特殊字符包含到字符串数组中。同时使用正则表达式模式的特殊字符拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34015637/

相关文章:

java - Http 基本身份验证不适用于 Spring WS 和 WebServiceTemplate 凭据

java - 如何在java中动态实例化异步服务 worker

regex - 当组 1、2、3 或 4 之间没有实例时,如何匹配 (group2.*|^.*)group1?

java - 在JAVA中使用这个正则表达式有什么问题?

regex - 使用正则表达式从 IAM 策略解析 ARN

java - 如何将我的 rest API 映射到根上下文来提供来自 CXF/JAX-RS 的静态内容?

java - 求数组的最小值、最大值和平均值

java - 为什么以及何时使用基于 EJB 的 Web 服务?

python - re 模块中内联标志的语法

php - 正则表达式用逗号和空格分割字符串,但忽略内部引号和括号