1

Below code is what I am using to parse a href link from a file :

  def parseFile(){
    val source = scala.io.Source.fromFile("C:\\Users\\Adrian\\Desktop\\parsefile.txt")
    val lines = source.getLines
    val Pattern = "\\s*(?i)href\\s*=\\s*(\\\"([^\"]*\")|'[^']*'|([^'\">\\s]+))"

    for (line <- source.getLines) {
        println(line)
    line match {
        case Pattern(c) => println(c)
        case _ => None
    }
   }

    source.close ()
  }

A sample line from the parsefile :

  <A HREF="www.google.com" title="test">test</A>

But I receive this Eclipse compile time error :

Multiple markers at this line
    - not found: value c
    - value Pattern is not a case class constructor, nor does it have an unapply/unapplySeq method

How should 'c' be declared in order to access the capture group String ?

I'm basing above code on accepted answer from this question :

How to pattern match using regular expression in Scala? where the code used is :

val Pattern = "([a-cA-C])".r
word.firstLetter match {
   case Pattern(c) => c bound to capture group here
   case _ =>
}

Any suggestions on a alternative methods to parse the file welcome.

Community
  • 1
  • 1
blue-sky
  • 51,962
  • 152
  • 427
  • 752

2 Answers2

5

There is a notable difference between your code:

val Pattern = "\\s*(?i)href\\s*=\\s*(\\\"([^\"]*\")|'[^']*'|([^'\">\\s]+))"

and the example:

val Pattern = "([a-cA-C])".r

Note the ending .r. That converts the String to a regexp pattern. The regexp pattern has a unaplySeq method, so it works with pattern matching.

v6ak
  • 1,636
  • 2
  • 12
  • 27
2

The type of Pattern is String, but should be Regex. Simply add a call to .r to your pattern string.

drexin
  • 24,225
  • 4
  • 67
  • 81