Matching against a regular expression in Scala

前端 未结 4 1015
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 02:52

I fairly frequently match strings against regular expressions. In Java:

java.util.regex.Pattern.compile(\"\\w+\").matcher(\"this_is\").matches

Ouch. Scala has

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-01 03:36

    I usually use

    val regex = "...".r
    if (regex.findFirstIn(text).isDefined) ...
    

    but I think that is pretty awkward.

提交回复
热议问题