Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #28011
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: regex puzzle |
| Date | 2014-04-09 18:24 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <li4rsf$d0j$1@dont-email.me> (permalink) |
| References | <n0kbk99fhl1se3phk8o9d6t6j3t3jkahs2@4ax.com> <li4k28$us1$1@dont-email.me> <fskbk9pbj5ice2ke9p0la8j1kkpi5dssco@4ax.com> <fhlbk95kj7sp1i7cneh2v585dq1npe42os@4ax.com> |
On 4/9/2014 16:24, Roedy Green wrote:
> On Wed, 09 Apr 2014 16:14:45 -0700, Roedy Green
> <see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
> someone who said :
>
>> nope 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20
>>
>> hopefully a little more terse.
>>
>> and would not match 2099
>
> I think you need a new regex operator that collects characters 0-9,
> 0-9\. etc then parses a number, then compares it with low and high
> bound in the regex operator.
>
> It seems like such a simple problem.
>
> Regex gets foiled so easily, like parsing HTML tags when they don't
> appear in canonical order.
>
import java.util.regex.*;
public class test5 {
public static void main(String[] args) {
String sequence = "1..20";
String subject = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 20";
System.out.println(matchSeq(sequence,subject) ? "matches" : "no
match");
}
public static boolean matchSeq(String seq, String subject) {
Pattern p1 = Pattern.compile("(\\d+)\\.\\.(\\d+)");
Matcher m1 = p1.matcher(seq);
if (m1.matches()) {
int from = Integer.parseInt(m1.group(1));
int to = Integer.parseInt(m1.group(2));
String s2 = Integer.toString(from);
for (int i=from+1; i<=to; i++)
s2 = s2.concat(String.format(" %d",i));
Pattern p2 = Pattern.compile(s2);
Matcher m2 = p2.matcher(subject);
return m2.matches();
} else {
throw new IllegalArgumentException("bad sequence");
}
}
}
--
Knute Johnson
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
regex puzzle Roedy Green <see_website@mindprod.com.invalid> - 2014-04-09 15:57 -0700
Re: regex puzzle Knute Johnson <eternal@knutejohnson.com> - 2014-04-09 16:11 -0700
Re: regex puzzle Roedy Green <see_website@mindprod.com.invalid> - 2014-04-09 16:14 -0700
Re: regex puzzle Roedy Green <see_website@mindprod.com.invalid> - 2014-04-09 16:24 -0700
Re: regex puzzle Knute Johnson <eternal@knutejohnson.com> - 2014-04-09 18:24 -0700
Re: regex puzzle Arne Vajhøj <arne@vajhoej.dk> - 2014-04-11 21:06 -0400
Re: regex puzzle markspace <markspace@nospam.nospam> - 2014-04-09 19:07 -0700
Re: regex puzzle Robert Klemme <shortcutter@googlemail.com> - 2014-04-11 01:56 -0700
csiph-web