TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SCALA

Scala Regex, R Examples: findFirstMatchIn

Use the Regex class to match patterns. Call findFirstMatchIn and other methods.
R, Regex. Some text operations are simple. We compare a string. We test a character. But for complex things, a Regex provides more power and is more effective.
With r, a function on strings, we create a regular expression from a string (a pattern). We then invoke methods that find matches on that pattern.
FindFirstMatchIn example. Here we create a Regex object from a pattern string with r(). Please note how we specify the Regex pattern—we use three quotes, which is a raw string literal.

For: We use a for-loop over the strings in our list. We call findFirstMatchIn on each string.

ForString

Result: The Regex matches strings that have one or more digits followed by the word "pounds."

Scala program that uses r, findFirstMatchIn // Get Regex from this string. val test = """\d* pounds""".r // A list of strings to match. val data = List("20 pounds", "5 pounds", "Error") // Loop over strings in the list. for (v <- data) { // Try to match the strings. val result = test.findFirstMatchIn(v) if (result.isDefined) { println(v + " = true") } else { println(v + " = false") } } Output 20 pounds = true 5 pounds = true Error = false
Groups. These help us label and access parts of a string. We use parentheses around the parts of our Regex we want to be placed in groups. And we pass group names to the r() method.

Result: We test isDefined to see if the option has an inner value. We use "get" to access the inner Regex.

And: We call the group() function, with string arguments, to access the named groups from our Regex object.

Scala program that uses Regex groups // An example string. val animals = "cat and dog" // Match words surrounding "and" and give them group names. val result = """(\w+) and (\w+)""".r( "animal1", "animal2").findFirstMatchIn(animals) // See if match occurred. if (result.isDefined) { // Get the Regex. val regex = result.get // Get named groups and display them. val animal1 = regex.group("animal1") val animal2 = regex.group("animal2") println(animal1) println(animal2) } Output cat dog
FindAllIn. This program uses findAllIn to get a MatchIterator from a Regex. Here we match all substrings in the string that start with a lowercase "a" or "b" and are followed by 2 digits.

For: We use a for-in loop to iterate over the results of MatchIterator. We access and print each string match.

Scala program that uses findAllIn // Contains space-separated ID codes. val ids = "a_1 a21 a31 b21 b_3 c10" // Match all occurrences starting with "a" or "b" and with 2 digits. val result = """[ab]\d\d""".r.findAllIn(ids) // Iterate over our result. // ... This accesses the Regex.MatchIterator. for (r <- result) { println(r) } Output a21 a31 b21
A summary. Scala provides pattern matching for variables with the "match" keyword. But for text, we can use regular expressions. We create them with the r() function.Match
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf