I'm trying to figure out how to use delimiter patterns in Java. I have tried googling and searching stack overflow but I can't find anything that answers my questions. How do you set patterns for useDelimiter? I'm trying to set a delimiter that separates text once it finds <
and delimits to >
. So in a string of text "this is <my> text"
it will separate "this is "
, "<my>"
, "text"
. I tried:
我想弄明白如何在Java中使用分隔符模式。我尝试过搜索和搜索stack overflow,但是我找不到任何能回答我的问题的东西。如何为useDelimiter设置模式?我试图设置一个分隔符,它在发现 <并将其递归到> 时分隔文本。在一个文本字符串中"this is
src.useDelimiter("<->");
and many other combinations. I see people using []
and ^
, but I don't know what this means. Is there some guide for making these patterns?
和许多其他的组合。我看到人们使用[]和^,但是我不知道这意味着什么。制作这些图案有什么指导吗?
1 个解决方案
#1
3
src.useDelimiter(" (?=<)|(?<=>) ")
Pattern A(?=B)
matches A followed by B, but matches only A.
模式A(?=B)匹配后面的B,但只匹配A。
Pattern (?<=A)B
matches A followd by B, but matches only B.
模式(?<=A)B与B匹配,但只匹配B。
See Special constructs (named-capturing and non-capturing) in https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
参见https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html中的特殊构造(命名捕获和非捕获)
#1
3
src.useDelimiter(" (?=<)|(?<=>) ")
Pattern A(?=B)
matches A followed by B, but matches only A.
模式A(?=B)匹配后面的B,但只匹配A。
Pattern (?<=A)B
matches A followd by B, but matches only B.
模式(?<=A)B与B匹配,但只匹配B。
See Special constructs (named-capturing and non-capturing) in https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
参见https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html中的特殊构造(命名捕获和非捕获)