Scala regex for string extraction between . and whitespace
up vote
0
down vote
favorite
I have the following string:
val str = "tagged.big AND tagged.medium"
I need to implement regex that will find all assurance of tagged.
till the first whitespace or the end of the line. In the current str
I expect to extract 2 strings:
tagged.big
tagged.medium
This is my current attempt:
val pattern = "tagged.*\s".r
but it returns:
Some(tagged.big AND )
Could you please show the proper regexp for this case?
regex scala
add a comment |
up vote
0
down vote
favorite
I have the following string:
val str = "tagged.big AND tagged.medium"
I need to implement regex that will find all assurance of tagged.
till the first whitespace or the end of the line. In the current str
I expect to extract 2 strings:
tagged.big
tagged.medium
This is my current attempt:
val pattern = "tagged.*\s".r
but it returns:
Some(tagged.big AND )
Could you please show the proper regexp for this case?
regex scala
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following string:
val str = "tagged.big AND tagged.medium"
I need to implement regex that will find all assurance of tagged.
till the first whitespace or the end of the line. In the current str
I expect to extract 2 strings:
tagged.big
tagged.medium
This is my current attempt:
val pattern = "tagged.*\s".r
but it returns:
Some(tagged.big AND )
Could you please show the proper regexp for this case?
regex scala
I have the following string:
val str = "tagged.big AND tagged.medium"
I need to implement regex that will find all assurance of tagged.
till the first whitespace or the end of the line. In the current str
I expect to extract 2 strings:
tagged.big
tagged.medium
This is my current attempt:
val pattern = "tagged.*\s".r
but it returns:
Some(tagged.big AND )
Could you please show the proper regexp for this case?
regex scala
regex scala
asked Nov 10 at 14:58
alexanoid
6,8351075164
6,8351075164
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
The pattern tagged.S+
should work here. This would match tagged.
followed by one or more whitespace characters. Here is a demo:
Demo
This is how I would write the pattern. The problem with your current pattern is that the .*
is greedy, and will keep consuming as much as possible before hitting a whitespace character. Also, in the case of the final match, tagged.medium
, there is no whitespace character which occurs after it. So, we could try using this:
tagged.*?(?=s|$)
This also works.
add a comment |
up vote
0
down vote
Expanding on @Tim Biegeleisen's Regex solution, here's one way to extract the substrings:
val str = "tagged.big AND tagged.medium"
val pattern = """(tagged.S+)""".r
pattern.findAllIn(str).matchData.flatMap(_.subgroups).toList
// res1: List[String] = List(tagged.big, tagged.medium)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The pattern tagged.S+
should work here. This would match tagged.
followed by one or more whitespace characters. Here is a demo:
Demo
This is how I would write the pattern. The problem with your current pattern is that the .*
is greedy, and will keep consuming as much as possible before hitting a whitespace character. Also, in the case of the final match, tagged.medium
, there is no whitespace character which occurs after it. So, we could try using this:
tagged.*?(?=s|$)
This also works.
add a comment |
up vote
1
down vote
accepted
The pattern tagged.S+
should work here. This would match tagged.
followed by one or more whitespace characters. Here is a demo:
Demo
This is how I would write the pattern. The problem with your current pattern is that the .*
is greedy, and will keep consuming as much as possible before hitting a whitespace character. Also, in the case of the final match, tagged.medium
, there is no whitespace character which occurs after it. So, we could try using this:
tagged.*?(?=s|$)
This also works.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The pattern tagged.S+
should work here. This would match tagged.
followed by one or more whitespace characters. Here is a demo:
Demo
This is how I would write the pattern. The problem with your current pattern is that the .*
is greedy, and will keep consuming as much as possible before hitting a whitespace character. Also, in the case of the final match, tagged.medium
, there is no whitespace character which occurs after it. So, we could try using this:
tagged.*?(?=s|$)
This also works.
The pattern tagged.S+
should work here. This would match tagged.
followed by one or more whitespace characters. Here is a demo:
Demo
This is how I would write the pattern. The problem with your current pattern is that the .*
is greedy, and will keep consuming as much as possible before hitting a whitespace character. Also, in the case of the final match, tagged.medium
, there is no whitespace character which occurs after it. So, we could try using this:
tagged.*?(?=s|$)
This also works.
answered Nov 10 at 15:03
Tim Biegeleisen
208k1379127
208k1379127
add a comment |
add a comment |
up vote
0
down vote
Expanding on @Tim Biegeleisen's Regex solution, here's one way to extract the substrings:
val str = "tagged.big AND tagged.medium"
val pattern = """(tagged.S+)""".r
pattern.findAllIn(str).matchData.flatMap(_.subgroups).toList
// res1: List[String] = List(tagged.big, tagged.medium)
add a comment |
up vote
0
down vote
Expanding on @Tim Biegeleisen's Regex solution, here's one way to extract the substrings:
val str = "tagged.big AND tagged.medium"
val pattern = """(tagged.S+)""".r
pattern.findAllIn(str).matchData.flatMap(_.subgroups).toList
// res1: List[String] = List(tagged.big, tagged.medium)
add a comment |
up vote
0
down vote
up vote
0
down vote
Expanding on @Tim Biegeleisen's Regex solution, here's one way to extract the substrings:
val str = "tagged.big AND tagged.medium"
val pattern = """(tagged.S+)""".r
pattern.findAllIn(str).matchData.flatMap(_.subgroups).toList
// res1: List[String] = List(tagged.big, tagged.medium)
Expanding on @Tim Biegeleisen's Regex solution, here's one way to extract the substrings:
val str = "tagged.big AND tagged.medium"
val pattern = """(tagged.S+)""".r
pattern.findAllIn(str).matchData.flatMap(_.subgroups).toList
// res1: List[String] = List(tagged.big, tagged.medium)
answered Nov 10 at 16:11
Leo C
9,3572616
9,3572616
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240172%2fscala-regex-for-string-extraction-between-and-whitespace%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown