Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.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_String_Split_Compilation - Fatal编程技术网

在java中,如何在获取偏移量值的同时拆分空格和特殊字符上的字符串

在java中,如何在获取偏移量值的同时拆分空格和特殊字符上的字符串,java,regex,string,split,compilation,Java,Regex,String,Split,Compilation,我正在尝试拆分/匹配标点符号和空格上的字符串,还需要获得偏移值 我住在印度 我希望输出像-[“我”,“生活”,“在”,“在”,“印度”,“在”] 以及每个令牌的开始和结束索引值 我试过使用- String text=“我住在印度。” 这将给出期望的结果,但是我可以将这两种模式组合在一个模式中吗 输出: I => 0 live => 2 , => 6 in => 8 India => 11 . => 16 正则表达式的解释: I => 0 live =&

我正在尝试拆分/匹配标点符号和空格上的字符串,还需要获得偏移值

我住在印度

我希望输出像-[“我”,“生活”,“在”,“在”,“印度”,“在”] 以及每个令牌的开始和结束索引值

我试过使用-

String text=“我住在印度。”

这将给出期望的结果,但是我可以将这两种模式组合在一个模式中吗

输出:

I => 0
live => 2
, => 6
in => 8
India => 11
. => 16
正则表达式的解释:

I => 0
live => 2
, => 6
in => 8
India => 11
. => 16
  • \b
    指定
  • |
    指定
  • \p{Punct}
    指定
  • \S+
    指定非空白字符

  • \S
    也会匹配
    \p{Punct}
    匹配的内容,因此您可能会有获取太多的风险,尽管这些情况可能很少见。感谢@WiktorStribiżew提供宝贵的反馈。
    I => 0
    live => 2
    , => 6
    in => 8
    India => 11
    . => 16