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.
|
|