All first numbers after some string pattern using regex in R
up vote
1
down vote
favorite
I want to extract all numbers after string "mystr" and something else. For example if I have string.
x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
It should return 8 and 12.
In R I tried:
stringr::str_extract_all(x, "mystr.*\d+")
r regex stringr
add a comment |
up vote
1
down vote
favorite
I want to extract all numbers after string "mystr" and something else. For example if I have string.
x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
It should return 8 and 12.
In R I tried:
stringr::str_extract_all(x, "mystr.*\d+")
r regex stringr
This returns only first number, not all of them
– Mislav
Nov 8 at 22:06
Well it seems you have multiplemystrin your input string. Then you may need to turn your dot-star into its un-greedy versionmystr.*?(\d+)
– revo
Nov 8 at 22:07
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to extract all numbers after string "mystr" and something else. For example if I have string.
x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
It should return 8 and 12.
In R I tried:
stringr::str_extract_all(x, "mystr.*\d+")
r regex stringr
I want to extract all numbers after string "mystr" and something else. For example if I have string.
x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
It should return 8 and 12.
In R I tried:
stringr::str_extract_all(x, "mystr.*\d+")
r regex stringr
r regex stringr
asked Nov 8 at 21:58
Mislav
453318
453318
This returns only first number, not all of them
– Mislav
Nov 8 at 22:06
Well it seems you have multiplemystrin your input string. Then you may need to turn your dot-star into its un-greedy versionmystr.*?(\d+)
– revo
Nov 8 at 22:07
add a comment |
This returns only first number, not all of them
– Mislav
Nov 8 at 22:06
Well it seems you have multiplemystrin your input string. Then you may need to turn your dot-star into its un-greedy versionmystr.*?(\d+)
– revo
Nov 8 at 22:07
This returns only first number, not all of them
– Mislav
Nov 8 at 22:06
This returns only first number, not all of them
– Mislav
Nov 8 at 22:06
Well it seems you have multiple
mystr in your input string. Then you may need to turn your dot-star into its un-greedy version mystr.*?(\d+)– revo
Nov 8 at 22:07
Well it seems you have multiple
mystr in your input string. Then you may need to turn your dot-star into its un-greedy version mystr.*?(\d+)– revo
Nov 8 at 22:07
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You may extract the closest digit chunks after mystr using
x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
regmatches(x, gregexpr("mystr.*?\K\d+", x, perl=TRUE))
# => [[1]]
# [1] "8" "12"
See the R demo
This PCRE regex will match
mystr-mystr
.*?- any 0+ chars other than line break chars as few as possible
\K- will omit the text matched so far
\d+- 1+ digits.
See the PCRE regex demo.
If you want to use stringr, you may use str_match_all:
> library(stringr)
> x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
> str_match_all(x, "mystr.*?(\d+)")[[1]][,2]
[1] "8" "12"
were the digits are captured into Group 1.
add a comment |
up vote
1
down vote
Sometimes str_match is more flexible than str_extract:
library(stringr)
str_match_all("This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12.",
"mystring.*?(\d+)")[[1]][, 2]
[1] "8" "12"
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You may extract the closest digit chunks after mystr using
x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
regmatches(x, gregexpr("mystr.*?\K\d+", x, perl=TRUE))
# => [[1]]
# [1] "8" "12"
See the R demo
This PCRE regex will match
mystr-mystr
.*?- any 0+ chars other than line break chars as few as possible
\K- will omit the text matched so far
\d+- 1+ digits.
See the PCRE regex demo.
If you want to use stringr, you may use str_match_all:
> library(stringr)
> x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
> str_match_all(x, "mystr.*?(\d+)")[[1]][,2]
[1] "8" "12"
were the digits are captured into Group 1.
add a comment |
up vote
2
down vote
accepted
You may extract the closest digit chunks after mystr using
x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
regmatches(x, gregexpr("mystr.*?\K\d+", x, perl=TRUE))
# => [[1]]
# [1] "8" "12"
See the R demo
This PCRE regex will match
mystr-mystr
.*?- any 0+ chars other than line break chars as few as possible
\K- will omit the text matched so far
\d+- 1+ digits.
See the PCRE regex demo.
If you want to use stringr, you may use str_match_all:
> library(stringr)
> x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
> str_match_all(x, "mystr.*?(\d+)")[[1]][,2]
[1] "8" "12"
were the digits are captured into Group 1.
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You may extract the closest digit chunks after mystr using
x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
regmatches(x, gregexpr("mystr.*?\K\d+", x, perl=TRUE))
# => [[1]]
# [1] "8" "12"
See the R demo
This PCRE regex will match
mystr-mystr
.*?- any 0+ chars other than line break chars as few as possible
\K- will omit the text matched so far
\d+- 1+ digits.
See the PCRE regex demo.
If you want to use stringr, you may use str_match_all:
> library(stringr)
> x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
> str_match_all(x, "mystr.*?(\d+)")[[1]][,2]
[1] "8" "12"
were the digits are captured into Group 1.
You may extract the closest digit chunks after mystr using
x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
regmatches(x, gregexpr("mystr.*?\K\d+", x, perl=TRUE))
# => [[1]]
# [1] "8" "12"
See the R demo
This PCRE regex will match
mystr-mystr
.*?- any 0+ chars other than line break chars as few as possible
\K- will omit the text matched so far
\d+- 1+ digits.
See the PCRE regex demo.
If you want to use stringr, you may use str_match_all:
> library(stringr)
> x <- "This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12."
> str_match_all(x, "mystr.*?(\d+)")[[1]][,2]
[1] "8" "12"
were the digits are captured into Group 1.
answered Nov 8 at 22:06
Wiktor Stribiżew
304k16123200
304k16123200
add a comment |
add a comment |
up vote
1
down vote
Sometimes str_match is more flexible than str_extract:
library(stringr)
str_match_all("This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12.",
"mystring.*?(\d+)")[[1]][, 2]
[1] "8" "12"
add a comment |
up vote
1
down vote
Sometimes str_match is more flexible than str_extract:
library(stringr)
str_match_all("This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12.",
"mystring.*?(\d+)")[[1]][, 2]
[1] "8" "12"
add a comment |
up vote
1
down vote
up vote
1
down vote
Sometimes str_match is more flexible than str_extract:
library(stringr)
str_match_all("This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12.",
"mystring.*?(\d+)")[[1]][, 2]
[1] "8" "12"
Sometimes str_match is more flexible than str_extract:
library(stringr)
str_match_all("This is mystring hola 8 and this yourstring hola 9 and again mystrings op 12.",
"mystring.*?(\d+)")[[1]][, 2]
[1] "8" "12"
answered Nov 8 at 22:10
neilfws
17.5k53648
17.5k53648
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53216787%2fall-first-numbers-after-some-string-pattern-using-regex-in-r%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
This returns only first number, not all of them
– Mislav
Nov 8 at 22:06
Well it seems you have multiple
mystrin your input string. Then you may need to turn your dot-star into its un-greedy versionmystr.*?(\d+)– revo
Nov 8 at 22:07