Java 如何使用正则表达式作为 stringtokenizer 的分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18852318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:37:51  来源:igfitidea点击:

how to use regex as delimiter for stringtokenizer

javaregex

提问by JungJoo

I want to retrieve the string between |2~ and |

我想检索之间的字符串 |2~ and |

What is the regex sequence to make this work?

使这项工作的正则表达式序列是什么?

Supposedly I have |2~21381030213jafjs231|adfafafasdfasAnd I am using

据说我有|2~21381030213jafjs231|adfafafasdfas而我正在使用

StringTokenizer test = new StringTokenizer(string, delim);

I tried StringTokenizer(string, "//|/2/~%s/|") // doesn't work.

我试过 StringTokenizer(string, "//|/2/~%s/|") // 不起作用。

but it doesn't work.

但它不起作用。

Can you also elaborate how regex work (like how regex sequence works)? Or good link to start looking into.

您能否详细说明正则表达式的工作原理(例如正则表达式序列的工作原理)?或开始研究的好链接。

Thank you

谢谢

采纳答案by Mike Perrenoud

If you need the Regex, .*~(.*)\|will do. And here is a Regex 101 to prove it.Now, to explain the Regex, it's pretty simple:

如果你需要正则表达式,.*~(.*)\|就可以了。这是一个正则表达式 101 来证明它。现在,要解释正则表达式,它非常简单:

  • .*tells the engine to match all characters;
  • ~then tells the engine to stop when it finds a ~;
  • (.*)then tells the engine to match all characters and group them as a match;
  • \|then finally tells the engine to match the |which will stop the grouping.
  • .*告诉引擎匹配所有字符;
  • ~然后告诉引擎在找到时停止~
  • (.*)然后告诉引擎匹配所有字符并将它们分组为一个匹配项;
  • \|然后最后告诉引擎匹配|这将停止分组。

But to learn Regex, have a look at this site, it's one of the best on the web.

但是要学习 Regex,请查看此站点,它是网络上最好的站点之一。

回答by shyam

StringTokenizerdoes not support regex. You need to use the Pattern& Matcherclasses

StringTokenizer不支持正则表达式。您需要使用Pattern&Matcher

String  s = "|2~21381030213jafjs231|adfafafasdfas";
Matcher m = Pattern.compile("\|\d~([^|]+)\|").matcher(s);
if ( m.find() ) {
  System.out.println(m.group(1));
}