- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
regex in scala interpreter
Thu, 2011-07-14, 21:45
I'm trying to use regex in a simple script (splitter.scala) using
the latest version of scala
>> Scala code runner version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL
****BEGIN SCALA SCRIPT****
val data = "aaaaabbbbbbccccccc"
val p = "^(.+)(.+)(.+)$".r
val parts = p.findAllIn(data)
val aes = parts.group(1)
val bes = parts.group(2)
val ces = parts.group(3)
Console.println(aes)
****END SCALA SCRIPT****
executing it with
jfglez@Platea:~/Documentos/scala/m200$ scala splitter.scala
i get the following error:
java.lang.IllegalStateException: No match available
at java.util.regex.Matcher.start(Matcher.java:355)
at scala.util.matching.Regex$MatchIterator.start(Regex.scala:316)
at scala.util.matching.Regex$MatchData$class.group(Regex.scala:198)
at scala.util.matching.Regex$MatchIterator.group(Regex.scala:291)
at Main$$anon$1.(splitter.scala:8)
at Main$.main(splitter.scala:1)
at Main.main(splitter.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
....
but executing line by line the above script on a scala console,
the "script" works.
where is the problem with the script?.
Some advice will be welcome
jfglez
Fri, 2011-07-15, 04:47
#2
Re: regex in scala interpreter
You *really* shouldn't call "group" on a MatchIterator, no matter what
the API says. The problem is that, at the time you call "group", no
matching was made yet! You haven't called "next" or anything to make
the match happen. If you type it on REPL, it works because "hasNext",
used to print the iterator, causes the match to be made.
You should iterate over the result of findAllIn to use it -- foreach,
flatMap, map...
On Thu, Jul 14, 2011 at 17:38, Jose Francisco Gonzalez
wrote:
> I'm trying to use regex in a simple script (splitter.scala) using
> the latest version of scala
>
>>> Scala code runner version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL
>
> ****BEGIN SCALA SCRIPT****
> val data = "aaaaabbbbbbccccccc"
>
> val p = "^(.+)(.+)(.+)$".r
>
> val parts = p.findAllIn(data)
> val aes = parts.group(1)
> val bes = parts.group(2)
> val ces = parts.group(3)
>
> Console.println(aes)
> ****END SCALA SCRIPT****
>
> executing it with
>
> jfglez@Platea:~/Documentos/scala/m200$ scala splitter.scala
>
> i get the following error:
>
> java.lang.IllegalStateException: No match available
> at java.util.regex.Matcher.start(Matcher.java:355)
> at scala.util.matching.Regex$MatchIterator.start(Regex.scala:316)
> at scala.util.matching.Regex$MatchData$class.group(Regex.scala:198)
> at scala.util.matching.Regex$MatchIterator.group(Regex.scala:291)
> at Main$$anon$1.(splitter.scala:8)
> at Main$.main(splitter.scala:1)
> at Main.main(splitter.scala)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> ....
>
> but executing line by line the above script on a scala console,
> the "script" works.
>
> where is the problem with the script?.
>
> Some advice will be welcome
>
> jfglez
>
>
If you will be using more complex regexes, you might want to check out http://kenmcdonald.github.com/rex .
Cheers,Ken