Part 1 - Pattern match
Greedy will consume as much as possible.
//lazy
scala> """[A-Za-z]+?""".r.findAllIn("ordernumbers are orderone and ordertwo").toList
res7: List[String] = List(o, r, d, e, r, n, u, m, b, e, r, s, a, r, e, o, r, d, e, r, o, n, e, a, n, d, o, r, d, e, r, t, w, o)
//greedy
scala> """[A-Za-z]+""".r.findAllIn("ordernumbers are orderone and ordertwo").toList
res8: List[String] = List(ordernumbers, are, orderone, and, ordertwo)
note
+-------------------+-----------------+------------------------------+ | Greedy quantifier | Lazy quantifier | Description | +-------------------+-----------------+------------------------------+ | * | *? | Star Quantifier: 0 or more | | + | +? | Plus Quantifier: 1 or more |
has pattern anything followed by timeMillis followed by anything
// simple event
scala> val event = "{\"timeMillis\": 123456}"
event: String = {"timeMillis": 123456}
scala> import java.util.regex.Pattern
import java.util.regex.Pattern
// has pattern {"timeMillis" : number}
scala> Pattern.matches("\\{\"timeMillis\": [0-9]*\\}", event)
res3: Boolean = true
// has pattern anything followed by timeMillis followed by anything
scala> Pattern.matches(".*timeMillis.*", event)
res4: Boolean = true
has pattern anything followed by "timeMillis" followed by anything
//complex event
scala> val event = "{\"timeMillis\": 123456, \"body\":\"some body\"}"
event: String = {"timeMillis": 123456, "body":"some body"}
// has pattern anything followed by "timeMillis" followed by anything
scala> Pattern.matches(".*\"timeMillis\".*", event)
res5: Boolean = true
has pattern {white space followed by "timeMillis" followed by anything
scala> val event = "{ \"timeMillis\": 123456, \"body\":\"some body\"}"
event: String = { "timeMillis": 123456, "body":"some body"}
scala> Pattern.matches("\\{\\s*\"timeMillis\"", event)
res8: Boolean = false
scala> Pattern.matches("\\{\\s*\"timeMillis\".*", event)
res9: Boolean = true
Part 2 - alphanumeric regex
scala> val regex = "^[a-zA-Z0-9]*$"
regex: String = ^[a-zA-Z0-9]*$
scala> import java.util.regex.Pattern
import java.util.regex.Pattern
scala> val alphanumeric = Pattern.compile(regex)
alphanumeric: java.util.regex.Pattern = ^[a-zA-Z0-9]*$
scala> alphanumeric.matcher("some order number").find()
res7: Boolean = false
scala> alphanumeric.matcher("someordernumber").find()
res8: Boolean = true
scala> alphanumeric.matcher("some funcky mother").find()
res9: Boolean = false
explanation
^ - asserts position at start of the string (like in VIm)
$ - asserts position at the end of the string (like in VIm)
tests - https://regex101.com/r/Zwozov/2/
Part 3
extract a chars array
- extract a number between
:
scala> val matcher = Pattern.compile(".*:([0-9]{12}):.*").matcher("arn:aws:kinesis:us-west-2:033814027302:stream/my_event_stream")
matcher: java.util.regex.Matcher = java.util.regex.Matcher[pattern=.*:([0-9]{12}):.* region=0,76 lastmatch=]
scala> matcher.find()
res16: Boolean = true
scala> matcher.group(1)
res17: String = 033814027302
:
scala> val matcher = Pattern.compile(".*:([0-9]{12}):.*").matcher("arn:aws:kinesis:us-west-2:033814027302:stream/my_event_stream")
matcher: java.util.regex.Matcher = java.util.regex.Matcher[pattern=.*:([0-9]{12}):.* region=0,76 lastmatch=]
scala> matcher.find()
res16: Boolean = true
scala> matcher.group(1)
res17: String = 033814027302
explanation
.* starting with anything
() between
[0-9]{12} - 12 digits/ equivalent to \d{12}
.* - ending with anything
Also,
Pattern.matches("arn:aws:kinesis:us-west-2:[0-9]{12}:stream/stream", "arn:aws:kinesis:us-west-2:033814027302:stream/some_stream")
res18: Boolean = true
.* starting with anything
() between
[0-9]{12} - 12 digits/ equivalent to \d{12}
.* - ending with anything
Pattern.matches("arn:aws:kinesis:us-west-2:[0-9]{12}:stream/stream", "arn:aws:kinesis:us-west-2:033814027302:stream/some_stream")
res18: Boolean = true
reference - https://regex101.com/r/ByqHlf/2
No comments:
Post a Comment