3

I want to find a match (if any) for a regular expression on a string, but only testing after a given index. For example:

val p = "[a-zA-Z_][a-zA-Z0-9_]*".r
val t = "const pi = 3.141592"
val m = p.findFirstMatchIn(t, start=5) // not real syntax

That is, I want to ignore the first n = 5 characters of the string. Is there any way to do that?

1
  • 1
    p.findFirstMatchIn(t.drop(5)) Commented Oct 17, 2018 at 17:58

1 Answer 1

4

Scala's .r syntax is a thin wrapper around the original java.util.regex.{Pattern, Matcher} API. The original API provides more control. Use Matcher.region:

scala> val m = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_]*").matcher(t)
scala> m.region(5, t.size)
scala> m.find()
scala> m.group(0)
res6: String = pi
Sign up to request clarification or add additional context in comments.

Comments

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.