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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:35:40+00:00 2026-06-17T14:35:40+00:00

Scala regex works great under either of two conditions: unconditionally executed code: e.g. val

  • 0

Scala regex works great under either of two conditions:
unconditionally executed code:
e.g.

val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
val anotherregx = """([\w]+)\t([/\w+=\-!%# ]+)""".r
val lineregx(category, aaUrl, title) 

or if inside a case statement we consume the expressions (and don’t need them again..)

val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
line match {

case lineregx(category, aaUrl, title) => // do something with category, aaUrl and title in here!
case anotherRegx(category, aaUrl) => // do something different with category, aaUrl and title in here!
case _ => { println("did not match line %s".format(line)); return 0 }

}

But what about if i need to ‘surface’ the matches to variables outside of the case statement? Specifically the var’s shown below,

val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
var category = "" ; var aaUrl = "";var title = ""
line match {
case lineregx(category, aaUrl, title) => val lineregx(category, aaUrl, title) = line
case anotherRegx(category, aaUrl) => val lineregx(category, aaUrl) = line
case _ => { println("did not match line %s".format(line)); return 0 }
}
// Do something with category, aaUrl, title HERE after the case statement.

Problem is , the syntax for applying the lineregx/anotherregex makes those variables local to the case statement only.

  • 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-17T14:35:41+00:00Added an answer on June 17, 2026 at 2:35 pm

    Roughly,

    val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
    val (category, aaUrl, title) = line match {
      case lineregx(category, aaUrl, title) => (category, aaUrl, title)
      case anotherRegx(category, aaUrl) => (category, aaUrl, ???)
      case _ => { println("did not match line %s".format(line)); return 0 }
    }
    // Do something with category, aaUrl, title HERE after the case statement.
    

    But that code is quite disorganized. For one thing, there’s the question of the value of title for the second case. For another, there’s the early return. Instead, the code would probably be best organized like this:

    // method declaration
    // ...
      val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
      line match {
        case lineregx(category, aaUrl, title) => f(category, aaUrl, title)
        case anotherRegx(category, aaUrl)     => f(category, aaUrl, ???)
        case _ => 
          println("did not match line %s".format(line))
          0
      }
    }  // end of method
    
    def f(category: String, aaUrl: String, title: String): Int = {
      // Do something with category, aaUrl, title HERE
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Scala, I have a regular expression pattern match like this: val Regex =
I have two packrat parsers in scala: val symbols : PackratParser[String] = { |
I have the following code to do regex pattern-matching. class UserAgent(val ua: String) {
scala> val p = ab|ba|(ab)*a|(ba)*b.r p: scala.util.matching.Regex = ab|ba|(ab)*a|(ba)*b scala> val m = p.pattern.matcher
scala> import scala.collection.mutable.Buffer import scala.collection.mutable.Buffer scala> val b = Buffer(1, 2, 3) b: scala.collection.mutable.Buffer[Int]
scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:
scala> val l = List((1,2), (2,3)) l: List[(Int, Int)] = List((1,2), (2,3)) I can
scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala>
In scala.util.matching.Regex trait MatchData I see that there support for groupnames , I thought
I'm trying to capture parts of a multi-lined string with a regex in Scala.

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.