Regular
Expression pattern
matching in Scala
A regular expression, regex or regexp[1] (sometimes called a rational
expression)[2][3] is a sequence of characters that
define a search pattern. Usually this pattern is used by string searching
algorithms for "find" or "find and replace"
operations on strings,
or for input validation.
Some
examples of regex are as below :
1)
var
str1:String = "Sunil"
var
pattern = "[A-Za-z]*"
var res
= str1.matches(pattern)
println(res)
//Output
true
2)
var
str1:String = "Sunil123"
var
pattern = "[A-Za-z]*"
var res
= str1.matches(pattern)
println(res)
//Output
false
3)
var
str1:String = "Sunil123"
var
pattern = "[A-Za-z0-9]*"
var res
= str1.matches(pattern)
println(res)
//Output
true
4)
var
str1:String = "Sunil$%123"
var
pattern = "[A-Za-z$%0-9]*"
var res
= str1.matches(pattern)
println(res)
//Output
true
----------------------------------------------------------------------------------
Q) Match that string which is having exactly three character
A) [A-Za-z]{3}
Q) Match that string which is having Starting and Ending
Limits ?
A) [A-Za-z]{1,5} :=> Minimum 1
Character and Maximum 5 Character
Q) Write a regular expression which will match below pattern
12.333.44.56
2.3.4.5
A) [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}
or \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}
\\For space you have to include \s or provide space in
pattern matching.
----------------------------------------------------------------------------------
Syntax of Scala Regex
Where Java inherits many
features from Perl, Scala inherits the syntax of Scala regular expressions from
Java. Here’s the list of metacharacter syntax:
Subexpression
Matches
^
Beginning of line
$
End of line
.
Single character; with
option m, matches newline too
[…]
Single character in square
brackets
[^…]
Single character not in
square brackets
\\A
Beginning of string
\\z
End of string
\\Z
End of string except
allowable final line terminator
re*
Zero or more occurrences
of preceding expression
re+
One or more occurrences of
preceding expression
re?
Zero or one occurrences of
preceding expression
re{ n}
n occurrences of preceding
expression
re{ n,}
n or more occurrences of
preceding expression
re{ n, m}
At least n, at most m
occurrences of preceding expression
a|b
Either a or b
(re)
Groups regular
expressions; remembers matched text
(?: re)Scala Regex
Groups regular
expressions; doesn’t remember matched text
(?> re)
Independent pattern;
doesn’t backtrack
\\w
Word characters
\\W
Nonword characters
\\s
Whitespace; equivalent to
[\t\n\r\f]
\\S
Nonwhitespace
\\d
Digits; equivalent to
[0-9]
\\D
Nondigits
\\A
Beginning of string
\\Z
End of string; matches
till before newline, if any
\\z
End of string
\\G
Point where last match
ended
\\n
Beck-reference to capture
group number ‘n’
\\b
Word boundaries when
outside brackets; backspace (0x08) when inside brackets
\\B
Nonword boundaries
\\n, \\t,etc.
Newlines, carriage
returns, tabs, and so
\\Q
Escape (quote) all characters up to \\E
\\E
Ends quoting begun with
\\Q
Finding Patterns in Strings
Regular
Expression pattern
matching in Scala
A regular expression, regex or regexp[1] (sometimes called a rational
expression)[2][3] is a sequence of characters that
define a search pattern. Usually this pattern is used by string searching
algorithms for "find" or "find and replace"
operations on strings,
or for input validation.
Some
examples of regex are as below :
1)
var
str1:String = "Sunil"
var
pattern = "[A-Za-z]*"
var res
= str1.matches(pattern)
println(res)
//Output
true
2)
var
str1:String = "Sunil123"
var
pattern = "[A-Za-z]*"
var res
= str1.matches(pattern)
println(res)
//Output
false
3)
var
str1:String = "Sunil123"
var
pattern = "[A-Za-z0-9]*"
var res
= str1.matches(pattern)
println(res)
//Output
true
4)
var
str1:String = "Sunil$%123"
var
pattern = "[A-Za-z$%0-9]*"
var res
= str1.matches(pattern)
println(res)
//Output
true
----------------------------------------------------------------------------------
Q) Match that string which is having exactly three character
A) [A-Za-z]{3}
Q) Match that string which is having Starting and Ending
Limits ?
A) [A-Za-z]{1,5} :=> Minimum 1
Character and Maximum 5 Character
Q) Write a regular expression which will match below pattern
12.333.44.56
2.3.4.5
A) [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}
or \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}
\\For space you have to include \s or provide space in
pattern matching.
----------------------------------------------------------------------------------
Finding Patterns in Strings
----------------------------------------------------------------------------------
Syntax of Scala Regex
Where Java inherits many
features from Perl, Scala inherits the syntax of Scala regular expressions from
Java. Here’s the list of metacharacter syntax:
|
Subexpression
|
Matches
|
|
^
|
Beginning of line
|
|
$
|
End of line
|
|
.
|
Single character; with
option m, matches newline too
|
|
[…]
|
Single character in square
brackets
|
|
[^…]
|
Single character not in
square brackets
|
|
\\A
|
Beginning of string
|
|
\\z
|
End of string
|
|
\\Z
|
End of string except
allowable final line terminator
|
|
re*
|
Zero or more occurrences
of preceding expression
|
|
re+
|
One or more occurrences of
preceding expression
|
|
re?
|
Zero or one occurrences of
preceding expression
|
|
re{ n}
|
n occurrences of preceding
expression
|
|
re{ n,}
|
n or more occurrences of
preceding expression
|
|
re{ n, m}
|
At least n, at most m
occurrences of preceding expression
|
|
a|b
|
Either a or b
|
|
(re)
|
Groups regular
expressions; remembers matched text
|
|
(?: re)Scala Regex
|
Groups regular
expressions; doesn’t remember matched text
|
|
(?> re)
|
Independent pattern;
doesn’t backtrack
|
|
\\w
|
Word characters
|
|
\\W
|
Nonword characters
|
|
\\s
|
Whitespace; equivalent to
[\t\n\r\f]
|
|
\\S
|
Nonwhitespace
|
|
\\d
|
Digits; equivalent to
[0-9]
|
|
\\D
|
Nondigits
|
|
\\A
|
Beginning of string
|
|
\\Z
|
End of string; matches
till before newline, if any
|
|
\\z
|
End of string
|
|
\\G
|
Point where last match
ended
|
|
\\n
|
Beck-reference to capture
group number ‘n’
|
|
\\b
|
Word boundaries when
outside brackets; backspace (0x08) when inside brackets
|
|
\\B
|
Nonword boundaries
|
|
\\n, \\t,etc.
|
Newlines, carriage
returns, tabs, and so
|
|
\\Q
|
Escape (quote) all characters up to \\E
|
|
\\E
|
Ends quoting begun with
\\Q
|
Finding Patterns in Strings
Create a Regex object by invoking the
.r method on a String, and then use that pattern with findFirstIn when
you’re looking for one match, and findAllIn when looking for all matches.
//Code
var str="Scala is scalable and cool"
var pattern = "(S|s)cala".r
println(pattern findFirstIn(str))
println(((pattern findFirstIn(str)).get))
//Output :
Some(Scala)
Scala
//Code
var str="Scala is scalable and cool"
var pattern = "(S|s)cala".r
println(pattern findAllIn(str))
println(((pattern
findAllIn(str)).mkString(",")))
//Output
non-empty iterator
Scala,scala
//Code
val numPattern = "[0-9]+".r
val address = "123 Main Street Suite 101"
//val matches = numPattern.findAllIn(address)
//matches.foreach(println)
val matches = numPattern.findAllIn(address).toArray
//Output
Array(123,101)
Replacing Patterns in Strings
Create a Regex object by invoking the
.r method on a String, and then use that pattern with findFirstIn when
you’re looking for one match, and findAllIn when looking for all matches.
//Code
var str="Scala is scalable and cool"
var pattern = "(S|s)cala".r
println(pattern findFirstIn(str))
println(((pattern findFirstIn(str)).get))
//Output :
Some(Scala)
Scala
//Code
var str="Scala is scalable and cool"
var pattern = "(S|s)cala".r
println(pattern findAllIn(str))
println(((pattern
findAllIn(str)).mkString(",")))
//Output
non-empty iterator
Scala,scala
val numPattern = "[0-9]+".r
val address = "123 Main Street Suite 101"
//val matches = numPattern.findAllIn(address)
//matches.foreach(println)
val matches = numPattern.findAllIn(address).toArray
//Output
Array(123,101)
Replacing Patterns in Strings
Because a String is immutable, you
can’t perform find-and-replace operations directly on it, but you can create a
new String that contains the replaced contents.
//Code
var str="Scala is scalable and cool"
var pattern = "(S|s)cala".r
println(pattern replaceFirstIn(str,"Java"))
//Output
Java is scalable and cool
//Code
var str="Scala is scalable and cool"
var pattern = "(S|s)cala".r
println(pattern replaceAllIn(str,"Java"))
//Output
Java is Javable and cool
Scala
match expression like a switch statement
Because a String is immutable, you
can’t perform find-and-replace operations directly on it, but you can create a
new String that contains the replaced contents.
//Code
var str="Scala is scalable and cool"
var pattern = "(S|s)cala".r
println(pattern replaceFirstIn(str,"Java"))
//Output
Java is scalable and cool
//Code
var str="Scala is scalable and cool"
var pattern = "(S|s)cala".r
println(pattern replaceAllIn(str,"Java"))
//Output
Java is Javable and cool
Scala
match expression like a switch statement
To use a Scala match expression like a Java
switch statement, use this approach :
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "many" // By default
}
var res = matchTest(10)
println(res)
//Output
Many
Provide value of i and
below code will print respective match :
val month = i match {
case 1
=> "January"
case 2
=> "February"
case 3
=> "March"
case 4
=> "April"
case 5
=> "May"
case 6
=> "June"
case 7
=> "July"
case 8
=> "August"
case 9
=> "September"
case 10 => "October"
case 11 => "November"
case 12 => "December"
case _
=> "Invalid month"
// the default, catch-all
}
println(match)
To use a Scala match expression like a Java
switch statement, use this approach :
def matchTest(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "many" // By default
}
var res = matchTest(10)
println(res)
//Output
Many
Provide value of i and
below code will print respective match :
val month = i match {
case 1
=> "January"
case 2
=> "February"
case 3
=> "March"
case 4
=> "April"
case 5
=> "May"
case 6
=> "June"
case 7
=> "July"
case 8
=> "August"
case 9
=> "September"
case 10 => "October"
case 11 => "November"
case 12 => "December"
case _
=> "Invalid month"
// the default, catch-all
}
println(match)
No comments:
Post a Comment