Discussion:
Confusing Regex Behavior
Randy J. Ray
2018-12-04 19:16:03 UTC
Permalink
I must be doing something wrong here, but I cannot figure this out.

The following results in "nil" from re-matches:

(re-matches #"\[((\d+)-(\d+)-(\d+) (\d\d):(\d\d))\] (.*)" "[1518-05-27
00:42] falls asleep\n")

This, however, properly matches the line and produces the backreferences:

(re-find (re-matcher #"\[((\d+)-(\d+)-(\d+) (\d\d):(\d\d))\] (.*)"
"[1518-05-27 00:42] falls asleep\n"))

I've used re-matches many times, but this has me stumped. This is behaving
this way on both 1.8.0 and 1.9.0.

Randy
--
Randy J. Ray - ***@gmail.com - twitter.com/rjray
Silicon Valley Scale Modelers: http://www.svsm.org
Sunnyvale, CA
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Andy Fingerhut
2018-12-04 19:27:46 UTC
Permalink
The doc string for re-matches says that it
uses java.util.regex.Matcher.matches(). The Java doc page for the class
java.util.regex.Matcher [1] says "The matches
<https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#matches-->
method
attempts to match the entire input sequence against the pattern."

The doc string for re-find says that it
uses java.util.regex.Matcher.find(). On [1] you can find the statement "
The find
<https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#find-->
method
scans the input sequence looking for the next subsequence that matches the
pattern."

I haven't dug into your regex and string in detail, but most likely what is
happening is that the regex matches part of the string, but it doesn't
match the _entire_ string.

Andy

[1] https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html
Post by Randy J. Ray
I must be doing something wrong here, but I cannot figure this out.
(re-matches #"\[((\d+)-(\d+)-(\d+) (\d\d):(\d\d))\] (.*)" "[1518-05-27
00:42] falls asleep\n")
(re-find (re-matcher #"\[((\d+)-(\d+)-(\d+) (\d\d):(\d\d))\] (.*)"
"[1518-05-27 00:42] falls asleep\n"))
I've used re-matches many times, but this has me stumped. This is behaving
this way on both 1.8.0 and 1.9.0.
Randy
--
Silicon Valley Scale Modelers: http://www.svsm.org
Sunnyvale, CA
--
You received this message because you are subscribed to the Google Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Randy J. Ray
2018-12-04 20:36:54 UTC
Permalink
Oh, that might be it. The newline at the end of the string might be what is
throwing a wrench into things. Though, to be fair, when I used re-matches
yesterday the newline wasn't an issue.

Nonetheless, I can work with re-find/re-matcher for now.
Post by Andy Fingerhut
The doc string for re-matches says that it
uses java.util.regex.Matcher.matches(). The Java doc page for the class
java.util.regex.Matcher [1] says "The matches
<https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#matches--> method
attempts to match the entire input sequence against the pattern."
The doc string for re-find says that it
uses java.util.regex.Matcher.find(). On [1] you can find the statement "
The find
<https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#find--> method
scans the input sequence looking for the next subsequence that matches the
pattern."
I haven't dug into your regex and string in detail, but most likely what
is happening is that the regex matches part of the string, but it doesn't
match the _entire_ string.
Andy
[1] https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html
Post by Randy J. Ray
I must be doing something wrong here, but I cannot figure this out.
(re-matches #"\[((\d+)-(\d+)-(\d+) (\d\d):(\d\d))\] (.*)" "[1518-05-27
00:42] falls asleep\n")
(re-find (re-matcher #"\[((\d+)-(\d+)-(\d+) (\d\d):(\d\d))\] (.*)"
"[1518-05-27 00:42] falls asleep\n"))
I've used re-matches many times, but this has me stumped. This is
behaving this way on both 1.8.0 and 1.9.0.
Randy
--
Silicon Valley Scale Modelers: http://www.svsm.org
Sunnyvale, CA
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Randy J. Ray - ***@gmail.com - twitter.com/rjray
Silicon Valley Scale Modelers: http://www.svsm.org
Sunnyvale, CA
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Smith
2018-12-04 21:36:48 UTC
Permalink
You don't need to use re-matcher in that example - the output of re-find
with the regex and the string is identical. If you are using the matcher to
collect a series of matches in one string, you can also uses re-seq which
returns a lazy-seq of the matches of your regex in the string.
Post by Randy J. Ray
Oh, that might be it. The newline at the end of the string might be what
is throwing a wrench into things. Though, to be fair, when I used
re-matches yesterday the newline wasn't an issue.
Nonetheless, I can work with re-find/re-matcher for now.
Post by Andy Fingerhut
The doc string for re-matches says that it
uses java.util.regex.Matcher.matches(). The Java doc page for the class
java.util.regex.Matcher [1] says "The matches
<https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#matches--> method
attempts to match the entire input sequence against the pattern."
The doc string for re-find says that it
uses java.util.regex.Matcher.find(). On [1] you can find the statement "
The find
<https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#find--> method
scans the input sequence looking for the next subsequence that matches the
pattern."
I haven't dug into your regex and string in detail, but most likely what
is happening is that the regex matches part of the string, but it doesn't
match the _entire_ string.
Andy
[1]
https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html
Post by Randy J. Ray
I must be doing something wrong here, but I cannot figure this out.
(re-matches #"\[((\d+)-(\d+)-(\d+) (\d\d):(\d\d))\] (.*)" "[1518-05-27
00:42] falls asleep\n")
(re-find (re-matcher #"\[((\d+)-(\d+)-(\d+) (\d\d):(\d\d))\] (.*)"
"[1518-05-27 00:42] falls asleep\n"))
I've used re-matches many times, but this has me stumped. This is
behaving this way on both 1.8.0 and 1.9.0.
Randy
--
Silicon Valley Scale Modelers: http://www.svsm.org
Sunnyvale, CA
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with
your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Silicon Valley Scale Modelers: http://www.svsm.org
Sunnyvale, CA
--
You received this message because you are subscribed to the Google Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Randy J. Ray
2018-12-04 22:16:23 UTC
Permalink
Oh, handy to know. Thanks.

(I come from a mostly Perl background, where regular expressions are
first-class objects and operations on/with them are baked in. The Java
approach to regexp that Clojure builds on is a little odd to me. But then,
Perl is more than a little odd to a lot of people...)

Randy
Post by Justin Smith
You don't need to use re-matcher in that example - the output of re-find
with the regex and the string is identical. If you are using the matcher to
collect a series of matches in one string, you can also uses re-seq which
returns a lazy-seq of the matches of your regex in the string.
Post by Randy J. Ray
Oh, that might be it. The newline at the end of the string might be what
is throwing a wrench into things. Though, to be fair, when I used
re-matches yesterday the newline wasn't an issue.
Nonetheless, I can work with re-find/re-matcher for now.
Post by Andy Fingerhut
The doc string for re-matches says that it
uses java.util.regex.Matcher.matches(). The Java doc page for the class
java.util.regex.Matcher [1] says "The matches
<https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#matches--> method
attempts to match the entire input sequence against the pattern."
The doc string for re-find says that it
uses java.util.regex.Matcher.find(). On [1] you can find the statement "
The find
<https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#find--> method
scans the input sequence looking for the next subsequence that matches the
pattern."
I haven't dug into your regex and string in detail, but most likely what
is happening is that the regex matches part of the string, but it doesn't
match the _entire_ string.
Andy
[1]
https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html
Post by Randy J. Ray
I must be doing something wrong here, but I cannot figure this out.
(re-matches #"\[((\d+)-(\d+)-(\d+) (\d\d):(\d\d))\] (.*)" "[1518-05-27
00:42] falls asleep\n")
This, however, properly matches the line and produces the
(re-find (re-matcher #"\[((\d+)-(\d+)-(\d+) (\d\d):(\d\d))\] (.*)"
"[1518-05-27 00:42] falls asleep\n"))
I've used re-matches many times, but this has me stumped. This is
behaving this way on both 1.8.0 and 1.9.0.
Randy
--
Silicon Valley Scale Modelers: http://www.svsm.org
Sunnyvale, CA
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with
your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with
your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
Silicon Valley Scale Modelers: http://www.svsm.org
Sunnyvale, CA
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Randy J. Ray - ***@gmail.com - twitter.com/rjray
Silicon Valley Scale Modelers: http://www.svsm.org
Sunnyvale, CA
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Matching Socks
2018-12-05 00:49:08 UTC
Permalink
See also Java's regex "Comparison to Perl 5" at
https://docs.oracle.com/javase/9/docs/api/java/util/regex/Pattern.html.

However, you are in for a treat! Perl is insanely great. Clojure is
simply great.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...