简体   繁体   中英

Regular expressions on Scala - Searching from the beginning of String

Currently, when I search for "th" it matches with "the" "these" and "other"...I don't want "other" because it is not at the beginning of the word.

I'm trying to use regular expressions on Scala and I am running into some problems. I am having trouble implementing "\\A" which matches at the beginning of a string.

I'm working on an autocomplete tool with the code seen below and currently I'm using contains...however it matches on any part of the word. What's the best way for it to only match from the start of the word using \\A?

val newList = ArrayBuffer[String]()
val pattern = new Regex (\\A searchText)

def searchList (searchText:String):Unit = {
    println("Search has been called")
    println(searchText)
    for(s <- words){
        println("for test")
        if(s contains(searchText)){
            println("if test")
            newList += s
             println(newList.mkString("\n"))
        }
    }
}

使用startsWith代替contains ,从字符串的开头搜索。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM