Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6876995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:32:49+00:00 2026-05-27T04:32:49+00:00

I fairly frequently match strings against regular expressions. In Java: java.util.regex.Pattern.compile(\w+).matcher(this_is).matches Ouch. Scala has

  • 0

I fairly frequently match strings against regular expressions. In Java:

java.util.regex.Pattern.compile(“\w+”).matcher(“this_is”).matches

Ouch. Scala has many alternatives.

  1. "\\w+".r.pattern.matcher("this_is").matches
  2. "this_is".matches("\\w+")
  3. "\\w+".r unapplySeq "this_is" isDefined
  4. val R = "\\w+".r; "this_is" match { case R() => true; case _ => false}

The first is just as heavy-weight as the Java code.

The problem with the second is that you can’t supply a compiled pattern ("this_is".matches("\\w+".r")). (This seems to be an anti-pattern since almost every time there is a method that takes a regex to compile there is an overload that takes a regex).

The problem with the third is that it abuses unapplySeq and thus is cryptic.

The fourth is great when decomposing parts of a regular expression, but is too heavy-weight when you only want a boolean result.

Am I missing an easy way to check for matches against a regular expression? Is there a reason why String#matches(regex: Regex): Boolean is not defined? In fact, where is String#matches(uncompiled: String): Boolean defined?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:32:49+00:00Added an answer on May 27, 2026 at 4:32 am

    You can define a pattern like this :

    scala> val Email = """(\w+)@([\w\.]+)""".r
    

    findFirstIn will return Some[String] if it matches or else None.

    scala> Email.findFirstIn("test@example.com")
    res1: Option[String] = Some(test@example.com)
    
    scala> Email.findFirstIn("test")
    rest2: Option[String] = None
    

    You could even extract :

    scala> val Email(name, domain) = "test@example.com"
    name: String = test
    domain: String = example.com
    

    Finally, you can also use conventional String.matches method (and even recycle the previously defined Email Regexp :

    scala> "david@example.com".matches(Email.toString)
    res6: Boolean = true
    

    Hope this will help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an ASP.NET application, I have a small number of fairly complex, frequently used
I have a fairly sophisticated server-side application which frequently requires me to see what
Technical question: Given a regex: my $regEx = qr{whatever$myVar}oxi; # Notice /o for compile-once
Given the need to Update Model from Database fairly frequently from the .edmx diagram,
Having released a new web application, updates are going out fairly frequently. Browsers cache
Fairly self-explanatory I feel
fairly new iPhone developer here. Building an app to send RS232 commands to a
Fairly exotic it seems to me. We recently upgraded/migrated from Windows Server 2003 to
Fairly simple issue which is solved in PHP by using a static variable. private
Being fairly new to JavaScript, I'm unable to discern when to use each of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.