由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - How to get a matched String?
相关主题
java regex pattern questionjava string stream
Regular Expression question: how to enumerate all matches?出个简单题,看你Java APi熟悉到什么程度
java StringA regular expression question
JAVA里什么METHOD是用来STRING PATTERN SEARCH大家都这么处理cross site scripting?
java 截取一部分stringregex急求帮助
向各位朋友请教一道题目must-have Eclipse plug-ins
Java怎麼做matcher呢?regular expression for whitespace in path
java问题:如何match两个正规表达式Do you guys use lex to parsing input?
相关话题的讨论汇总
话题: string话题: strs话题: matched话题: regex话题: get
进入Java版参与讨论
1 (共1页)
g*****g
发帖数: 34805
1
If you have a long text, you want to use regex to match it and
get back the first matched String, is this what I should do?
String[] strs = text.split("regex", 2);
text.substring(strs[0].length, text.length - strs[1].length);
t*******e
发帖数: 684
2
try Pattern and Matcher classes.
m******t
发帖数: 2416
3

But that only gets you the part _before_ the first matching substring.
Try something like Pattern.compile(regex).matcher(text).find()

【在 g*****g 的大作中提到】
: If you have a long text, you want to use regex to match it and
: get back the first matched String, is this what I should do?
: String[] strs = text.split("regex", 2);
: text.substring(strs[0].length, text.length - strs[1].length);

g*****g
发帖数: 34805
4
Actually this should work, coz str[0] is the part before, and
str[1] is the part after. But your way looks more elegant.

【在 m******t 的大作中提到】
:
: But that only gets you the part _before_ the first matching substring.
: Try something like Pattern.compile(regex).matcher(text).find()

m******t
发帖数: 2416
5

Oh, right, now that I read it again, the index jumping makes
sense.
...Yeah... I guess that shows the pattern approach _is_ at least
more readable. ;-)

【在 g*****g 的大作中提到】
: Actually this should work, coz str[0] is the part before, and
: str[1] is the part after. But your way looks more elegant.

1 (共1页)
进入Java版参与讨论
相关主题
Do you guys use lex to parsing input?java 截取一部分string
regex question?向各位朋友请教一道题目
简单算法问题Java怎麼做matcher呢?
类里的字符串向量赋值,总报错:java问题:如何match两个正规表达式
java regex pattern questionjava string stream
Regular Expression question: how to enumerate all matches?出个简单题,看你Java APi熟悉到什么程度
java StringA regular expression question
JAVA里什么METHOD是用来STRING PATTERN SEARCH大家都这么处理cross site scripting?
相关话题的讨论汇总
话题: string话题: strs话题: matched话题: regex话题: get