Reputation: 71
I want to capture all the numbers with G right after them ( I only want the number). I have tried the following but it won't give me the one followed right by G.
a="1.1G;1.2 G;1.435 G;1.1 G;1.5555G"
stringr::str_extract_all(a, "(?<!\\d)\\d+\\.*\\d*(?=\\s*G)\\b")
so I would want all the numbers extracted from the above because all the numbers are followed by G ( it doesn't matter if there is spacing between the numbers and the letter G)
so I would expect the extract to have:
1.1, 1.2,1.435,1.1,1.5555
Upvotes: 1
Views: 41
Reputation: 163362
You can omit the negative lookbehind as the match already starts with the first digit, and match digits with an optional decimal part asserting what is on the right is a G allowing optional whitespace chars.
\d+(?:\.\d+)?(?=\s*G)
a="1.1G;1.2 G;1.435 G;1.1 G;1.5555G"
stringr::str_extract_all(a, "\\d+(?:\\.\\d+)?(?=\\s*G\\b)")
Output
[[1]]
[1] "1.1" "1.2" "1.435" "1.1" "1.5555"
Upvotes: 3