Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #28005 > unrolled thread
| Started by | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| First post | 2014-04-09 15:57 -0700 |
| Last post | 2014-04-11 01:56 -0700 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-04-09 15:57 -0700 |
| Subject | regex puzzle |
| Message-ID | <n0kbk99fhl1se3phk8o9d6t6j3t3jkahs2@4ax.com> |
Is there a way within a regex to find strings of the from: 1 .. 20? For reasons hard to explain I don't want to capture then check outside the regex. -- Roedy Green Canadian Mind Products http://mindprod.com "Don't worry about people stealing an idea; if it's original, you'll have to shove it down their throats." ~ Howard Aiken (born: 1900-03-08 died: 1973-03-14 at age: 73)
[toc] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-04-09 16:11 -0700 |
| Message-ID | <li4k28$us1$1@dont-email.me> |
| In reply to | #28005 |
On 4/9/2014 15:57, Roedy Green wrote: > Is there a way within a regex to find strings of the from: > > 1 .. 20? > > For reasons hard to explain I don't want to capture then check outside > the regex. > So does 1 .. 20 mean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 or 1 .. 20? 1 .. 20 ^\d+ \.\. \d+$ -- Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-04-09 16:14 -0700 |
| Message-ID | <fskbk9pbj5ice2ke9p0la8j1kkpi5dssco@4ax.com> |
| In reply to | #28006 |
On Wed, 09 Apr 2014 16:11:04 -0700, Knute Johnson <eternal@knutejohnson.com> wrote, quoted or indirectly quoted someone who said : > >So does 1 .. 20 mean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 >or 1 .. 20? 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 -- Roedy Green Canadian Mind Products http://mindprod.com "Don't worry about people stealing an idea; if it's original, you'll have to shove it down their throats." ~ Howard Aiken (born: 1900-03-08 died: 1973-03-14 at age: 73)
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-04-09 16:24 -0700 |
| Message-ID | <fhlbk95kj7sp1i7cneh2v585dq1npe42os@4ax.com> |
| In reply to | #28007 |
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. -- Roedy Green Canadian Mind Products http://mindprod.com "Don't worry about people stealing an idea; if it's original, you'll have to shove it down their throats." ~ Howard Aiken (born: 1900-03-08 died: 1973-03-14 at age: 73)
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-04-09 18:24 -0700 |
| Message-ID | <li4rsf$d0j$1@dont-email.me> |
| In reply to | #28008 |
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
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2014-04-11 21:06 -0400 |
| Message-ID | <534891a7$0$296$14726298@news.sunsite.dk> |
| In reply to | #28008 |
On 4/9/2014 7:24 PM, 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. Sure but it is not a regex problem. Arne
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2014-04-09 19:07 -0700 |
| Message-ID | <li4ud5$phh$1@dont-email.me> |
| In reply to | #28006 |
On 4/9/2014 5:52 PM, Stefan Ram wrote:
> { final java.lang.String r = "[1-9]|1[0-9]|20";
This is the type of solution I'm used to seeing for problems of this
type. Roedy said he didn't want to match substrings (so "2009" should
not be matched. I think you just have to add some "not digit" to the
outside of that.
"\\D([1-9]|1[0-9]|20)\\D"
Not tested.
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2014-04-11 01:56 -0700 |
| Message-ID | <7b4a7c5e-a31d-405b-a121-56c0827af8c7@googlegroups.com> |
| In reply to | #28012 |
On Thursday, April 10, 2014 4:07:30 AM UTC+2, markspace wrote:
> This is the type of solution I'm used to seeing for problems of this
> type. Roedy said he didn't want to match substrings (so "2009" should
> not be matched. I think you just have to add some "not digit" to the
> outside of that.
>
> "\\D([1-9]|1[0-9]|20)\\D"
But better use lookaround for that:
package regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RoedyNumberMatch {
private static final String[] P_FIXES = { "", "foo", "bar ", "20" };
private static final Pattern PAT = Pattern
.compile("(?<!\\d)(?:20|1\\d|[1-9])(?!\\d)");
public static void main(String[] args) {
final Matcher m = PAT.matcher("");
for (int i = 0; i < 30; ++i) {
System.out.println("i = " + i);
for (final String pre : P_FIXES) {
for (final String post : P_FIXES) {
final String s = pre + i + post;
System.out.println("s = '" + s + "'");
if (m.reset(s).matches()) {
System.out.println("Matches: '" + m.group() + "'");
}
if (m.reset(s).find()) {
System.out.println("Finds: '" + m.group() + "'");
}
}
}
System.out.println();
}
}
}
Also here: https://gist.github.com/rklemme/10451386
Cheers
robert
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web