0

I am trying to extract few values out of an big string , I have an hard time extracting them , I have tired a couple of regex patterns , but they always give me no match. Anyway they seem to work in the online regex sites available but not in Scala. What I am trying to do is

Input :

ESSSTOR\Disk&Ven_VendorName&Prod_MO_Might_MS_5.0&Rev_6.01\08765J54U3K4QVR0&0

Extract [output]:

Vendorname

MO_Might_MS_5.0&Rev_6.01

08765J54U3K4QVR0&0

I am trying to extract those three values from the input string , but unable to do so.

Can some one please me see what I am doing wrong.

Thanks in advance.

//Input value

  val device:String=  "ESSSTOR\\Disk&Ven_VendorName&Prod_MO_Might_MS_5.0&Rev_6.01\\08765J54U3K4QVR0&0"

// Regex build for product extraction

           val proReg=  """.*[Prod_]([^\\\\]*)""".r
                     // """.*Prod_([^\\\\]*)""".r  -- no match as output
                    // """(?:Prod_)([^\\\\]*)""".r  -- no match as output

      println("Device: "+device)

    // method -1:   
    device match{

      case proReg(prVal) => println(s"$prVal is product val")
      case _ =>  println("no match")   }

    // method-2 : 

   val proReg(g1) = "ESSSTOR\\Disk&Ven_VendorName&Prod_MO_Might_MS_5.0&Rev_6.01\\08765J54U3K4QVR0&0"
   println(s"group1: $g1 ")


   O/P:


  Device:  ESSSTOR\Disk&Ven_VendorName&Prod_MO_Might_MS_5.0&Rev_6.01\08765J54U3K4QVR0&0

   //method-1

   no match

   // method-2

   error

// Regex build for dev serial

    val serReg = """(?:Prod_\\S*[\\\\])(.*)""".r

    device match {
      case serReg(srVal) => println(s"$srVal is product val")
      case _ => println("no match")
    }


    o/p:

    no match

// Regex for vendor

    val venReg="""(?:Ven_)([^&]*)""".r

    device match {
      case venReg(vnVal) => println(s"$vnVal is vendor val")
      case _ => println("no match")
    }


    o/p:

    no match
1
  • @jwvh : please help me see what I am doing wrong Commented Sep 14, 2017 at 16:43

1 Answer 1

2

See if this gets closer to what you want.

val pttrn = raw"Ven_([^&]+)&Prod_([^&]+)&Rev_6.01\\(.*)".r.unanchored

device match {
  case pttrn(ven, prod, rev) => 
      s"vendor: $ven\nproduct: $prod\nrevNum: $rev"
  case _ => "does not match pattern"
}

explanation

Ven_([^&]+) --> Look for something that begins with Ven_. Capture everything that isn't an ampersand &.

&Prod_([^&]+) --> That should be followed by the Prod_ string. Capture everything that isn't an ampersand &.

&Rev_6.01\\(.*) --> That should be followed by the Rev_ string that ends with a single backslash \. Capture everything that follows.

Sign up to request clarification or add additional context in comments.

4 Comments

it throws an Exception in thread "main" scala.MatchError: and there is no explanation to it.
@codehunter; It passes my tests. Check that the device string contains a single backslash \ separating Rev_6.01 from the target string.
It worked !! :) What ever you wrote is simple , I dont why I got myself into using non capturing groups and nothing worked. I did upvote and marked the answer as correct , as I have less points it is not shown in public.
We sure need better tooling for regexes besides SO and jwh and besides those worksheet sites.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.