This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Few methods in scala.util.Regex

2 replies
Michael Schmitz
Joined: 2011-11-01,
User offline. Last seen 42 years 45 weeks ago.

Is there a reason there are so few methods on scala.util.Regex? Would
anyone else like to see:

matches(string: String): Boolean
find(string: String): Boolean

I'm constantly juggling the existing methods to accomplish the above
simple tasks.

Peace. Michael

Tom Switzer
Joined: 2011-07-19,
User offline. Last seen 42 years 45 weeks ago.
Re: Few methods in scala.util.Regex
Truth be told, 90% of the time I use RegEx, I just pattern match:
val XRef = """([^:]*):(.*)""" r...xref match {  case XRef(dbName, dbKey) => ...   case _ => // can't match}

You could also create a little helper, like this:
implicit def regexHelper(re: Regex) = new {  def matches(x: String): Boolean = !(re findFirstIn x isEmpty) }
val re = """\w+""".r...if (re matches "hello") { ... }

On Fri, Dec 9, 2011 at 8:40 PM, Michael Schmitz <michael@schmitztech.com> wrote:
Is there a reason there are so few methods on scala.util.Regex?  Would
anyone else like to see:

matches(string: String): Boolean
find(string: String): Boolean

I'm constantly juggling the existing methods to accomplish the above
simple tasks.

Peace.  Michael

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Few methods in scala.util.Regex

On Fri, Dec 9, 2011 at 23:40, Michael Schmitz wrote:
> Is there a reason there are so few methods on scala.util.Regex?  Would
> anyone else like to see:
>
> matches(string: String): Boolean
> find(string: String): Boolean
>
> I'm constantly juggling the existing methods to accomplish the above
> simple tasks.

It bothers me a bit, but not much. The first one can be accomplished
by the "matches" method on String, even though it recompiles the
pattern every time. The second one is more bothersome.

I'd like to have =~ on Regex, or, perpahs, =~:, since I prefer regular
expressions to the right. I'd make =~: return Boolean (or replace),
and =~~: return Match. But I just don't find myself using these
particular patterns all that much. When I have to resort to regex, I
usually have to extract something, and then the code looks like this:

for (pattern(x, y, z) <- pattern findAllIn text) ....

Now, there's a third party regex library for Scala if you want
something with more power.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland