2

I have a String that looks like this and I want to extract the bit between the pipe and the tilde.

{>}D003 S/N|555555~EB

So the result of the parsing should give me 555555 back. Here is what I tried, but without success:

"""\D003 S/N|.*\~""".r.findFirstIn("{>}D003 S/N|555555~EB")

which gives me:

Some({>}D003 S/N|555555~)

1 Answer 1

2

You may use a simple unanchored regex with a capturing group: D003 S/N\|([^~]+)~.

See the Scala demo:

val rx = """D003 S/N\|([^~]+)~""".r.unanchored
val s = "{>}D003 S/N|555555~EB"
val res = s match { 
    case rx(c) => c
    case _ => ""
}
println(res)

Pattern details:

  • D003 S/N\| - match a literal char sequence D003 S/N|
  • ([^~]+) - capturing group 1 matching 1 or more chars other than ~
  • ~ - a literal char ~.
Sign up to request clarification or add additional context in comments.

4 Comments

But I could also have Strings that contain "{>}D004 S/N|555555~EB" instead of "{>}D003 S/N|555555~EB". So I would only like to parse the ones with a D003 S/N on it. How could I do that?
Check if the input string contains D003 S/N in it and only then extract the value. If the value comes before the number, just add it to the regex: """D003 S/N.*\|([^~]+)""".r.unanchored. See this demo.
But it still matches for messages without tild, "{>}D003 S/N|55555 EB. I want both conditions to be matched.
Nothing easier, just add it at the end of the pattern :) I will update the answer now, since you clarified the requirements. ... Done.

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.