Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion missing a good documentation
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Daniel Sobral  
View profile  
 More options Jul 1 2011, 10:34 am
From: Daniel Sobral <dcsob...@gmail.com>
Date: Fri, 1 Jul 2011 11:34:01 -0300
Local: Fri, Jul 1 2011 10:34 am
Subject: Re: [scala-user] missing a good documentation
2011/6/30 François Gannaz <francois.gan...@gmail.com>:

> About scala.util.Regex, I had a hard time to handle some simple tasks, like
> getting a collection of the groups for all the matches of a pattern in a given
> string. Something like ` push @a,$1 for /id=(\d+)/g ` in Perl.

I missed this comment... Yes, I know *exactly* how you feel. Some ways
of doing it:

val a = for {
  matched <- """id=(\d+)""".r.findAllIn(source).matchData.toList

} yield matched.subgroups(0)

val a = for {
  matched <- """id=(\d+)""".r.findAllIn(source).matchData.toList

} yield matched.group(1)

val a = for {
  matched <- """id=(\d+)""".r.findAllIn(source).matchData.toList
  groups <- matched.subgroups

} yield groups

val a = """id=(\d+)""".r.findAllIn(source).matchData.flatMap(_ subgroups).toList

// The following examples need this import
import scala.util.matching.Regex.Groups

val a = for {
  matched <- """id=(\d+)""".r.findAllIn(source).matchData.toList
  Groups(first, _ @ _*) = matched

} yield first

val a = for {
  Groups(first) <- """id=(\d+)""".r.findAllIn(source).matchData.toList

} yield first

// If you have many subgroups, but only care about the first
val a = for {
  Groups(first, _ @ _*) <- """id=(\d+)""".r.findAllIn(source).matchData.toList

} yield first

// The following example needs the pattern to be assigned to an identifier
val pattern = """id=(\d+)""".r
val a = for {
  pattern(first) <- pattern.findAllIn(source).matchData.toList

} yield first

--
Daniel C. Sobral

I travel to the future all the time.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.