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.

2 Answers 2

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.

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.