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 8996053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:42:07+00:00 2026-06-15T23:42:07+00:00

In Ruby, if I have two Regexps, I have the possibility to create another

  • 0

In Ruby, if I have two Regexps, I have the possibility to create another regexp like this:

a = /\d+/ # Matches digits
b = /\s+/ # Matches whitespaces
c = Regexp.union(a, b) # Matches sequences that consist only of digits or only of whitespaces

I want to do the same thing in Scala, but I didn’t find out how I could do that. Note that I am not asking for a syntax to create a union of character classes like (\d+)|(\s+) in the previous example, I am really looking for a possibility to create a new Regexp from two given Regexps.

Actually, in the end, I will not do it for just two Regexps but a large number. I don’t care about grouping or anything, I just want to know if a String matches one of a list of given Regexps. I could just check all of them in a loop, but that is too inefficient, that is why I need one Regexp to check the union.

  • 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-06-15T23:42:08+00:00Added an answer on June 15, 2026 at 11:42 pm

    Scala uses the Java regex engine, which is based on the class java.util.regex.Pattern. Pattern has exactly one method that can create a regex:

    public static Pattern compile(String regex)
    

    That’s it, and Scala doesn’t give you any relevant enhancements.

    But one thing you can do is use the built-in unioning in match statements, here shown with capturing groups in case you want to pull something out of the string:

    val Dig = """(\d+)""".r
    val Wsp = """(\s+)""".r
    
    scala> "45" match { case Dig(_) | Wsp(_) => println("found"); case _ => }
    

    found

    scala> "   " match { case Dig(_) | Wsp(_) => println("found"); case _ => }
    

    found

    If you really want a combined regex, you have to do it at the string level. You can get the java Pattern from a Scala regex with .pattern, and another .pattern then gets the string. Most regexes can be wrapped safely in (?:) to get a non-capturing block, so you can combine like so:

    val Both = ("(?:"+Dig.pattern.pattern+")|(?:"+Wsp.pattern.pattern+")").r
    

    However, any capturing groups inside will both be represented, but the non-used branch will be null (not exactly a good way to write idiomatic Scala, but anyway, this is what Java uses):

    scala> "2" match { case Both(d,w) => if (w!=null) println("white") else println(d) }
    2
    
    scala> " " match { case Both(d,w) => if (w!=null) println("white") else println(d) }
    white
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why do this Ruby object have two to_s and inspect methods that
I have two Ruby files: db/migrate/20120920180336_create_admin_pages.rb db/migrate/20120920150716_devise_create_users.rb There's a constant variable in this file:
I have two arrays like this: keys = ['a', 'b', 'c'] values = [1,
I have two Ruby arrays, and I need to see if they have any
In Ruby, I want to have two threads running at the same time, and
I have once again fleshed out Ruby, after two years of not touching it,
I'm learning ruby (have a java/groovy background) and it looks like when writing methods,
I have two ruby script cron jobs that I'm trying to run under Ubuntu
So, lets say I have two nearly identical classes in C# and Ruby: C#
I am creating a site in Ruby on Rails, I have two models a

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.