Home > Enterprise >  Regex for extracting two numbers with a between and end pattern
Regex for extracting two numbers with a between and end pattern

Time:10-15

I need to extract two numbers under certain conditions: cumulativelly, the first one need to be followed by a /A, AND the second one must be followed by a B. For instance:

  • 30A/305B: both 30 and 305 must be returned;
  • 30/305B: neither 30 nor 305 would be returned;
  • 30A/305: neither 30 nor 305 would be returned;

As per here, I tried the regex below without success:

(\d (?=A\/))(\d (?=B))

How do I get it done? I am using R , with option perl = TRUE.

CodePudding user response:

The pattern (\d (?=A\/))(\d (?=B)) matches 1 or more digits, and asserts (not match) A/ directly to the left.

That first assertion will be true, but after this assertion there is a second part that starts with matching 1 or more digits again.

That can not work, as the previous assertion just asserted that there is A/


You don't need any lookarounds or perl = TRUE, you can use 2 capture groups instead:

(\d )A/(\d )B

Regex demo

str <- "30A/305B: both 30 and 305 must be returned;"
str_match(str, "(\\d )A/(\\d )B")

Output

     [,1]       [,2] [,3] 
[1,] "30A/305B" "30" "305"
  • Related