Redian新闻
>
Regular Expression question: how to enumerate all matches?
avatar
Regular Expression question: how to enumerate all matches?# Java - 爪哇娇娃
G*a
1
I want to obtain all the possible matches of "a" from "aaaaa", that should be:
a
aa
aaa
aaaa
aaaaa
but "a+" does not do the the job, it only returns the longest match "aaaaa".
how can i get all the possible matches enumerated? Thanks!
avatar
g*g
2
You should write an algorithm to get all the substring first,
then you can match one by one.

be:
".

【在 G*********a 的大作中提到】
: I want to obtain all the possible matches of "a" from "aaaaa", that should be:
: a
: aa
: aaa
: aaaa
: aaaaa
: but "a+" does not do the the job, it only returns the longest match "aaaaa".
: how can i get all the possible matches enumerated? Thanks!

avatar
t*r
3
how about a{1,5} ?

I want to obtain all the possible matches of "a" from "aaaaa", that should
be:
a
aa
aaa
aaaa
aaaaa
but "a+" does not do the the job, it only returns the longest match "aaaaa".
how can i get all the possible matches enumerated? Thanks!

【在 G*********a 的大作中提到】
: I want to obtain all the possible matches of "a" from "aaaaa", that should be:
: a
: aa
: aaa
: aaaa
: aaaaa
: but "a+" does not do the the job, it only returns the longest match "aaaaa".
: how can i get all the possible matches enumerated? Thanks!

avatar
G*a
4
yi? you have a daughter now?! she is so so so chobby and cute!
nod, since i can't get the regex do the work, i am post-process the returned
string myself.
thanks!

【在 g*****g 的大作中提到】
: You should write an algorithm to get all the substring first,
: then you can match one by one.
:
: be:
: ".

avatar
G*a
5
thanks!
but it returns the longest one still:
aaaaa 0 5

".

【在 t**********r 的大作中提到】
: how about a{1,5} ?
:
: I want to obtain all the possible matches of "a" from "aaaaa", that should
: be:
: a
: aa
: aaa
: aaaa
: aaaaa
: but "a+" does not do the the job, it only returns the longest match "aaaaa".

avatar
g*e
6
works for me in vi

【在 G*********a 的大作中提到】
: thanks!
: but it returns the longest one still:
: aaaaa 0 5
:
: ".

avatar
F*n
7
If you want to do that in a program, you have to enumerate all substrings
and match them against the regex, because you don't have an index. There are
shortcuts.

returned

【在 G*********a 的大作中提到】
: yi? you have a daughter now?! she is so so so chobby and cute!
: nod, since i can't get the regex do the work, i am post-process the returned
: string myself.
: thanks!

avatar
G*a
8
avatar
B*g
9
co-ask,搞不出来

【在 G*********a 的大作中提到】

avatar
b*e
10
How about use group and loop.
import java.util.regex.*;
public class test
{
public static void main(String[] args)
{
Pattern p = Pattern.compile("a(a*)");
String a = "aaaa";
match(a, p);
}
private static void match(String s, Pattern p)
{
Matcher m = p.matcher(s);
while (m.find())
{
System.out.println("found char = " + m.group(0));
match(m.group(1), p);
}
}
}
avatar
a*i
11
in perl you can do @matched = "aaaaa" =~ m/(?=(a*))/g;
in java you probably can do similar thing by using zero-length look-ahead
and Matcher.find()
avatar
h*o
12
@matched = "aaaaa" =~ m/(?=(a*))/g;
is a good one if it works.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。