- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Few methods in scala.util.Regex
Sat, 2011-12-10, 02:40
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
Sat, 2011-12-10, 20:51
#2
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.
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: