正则表达式捕获组流入第二个匹配集

I am trying to match multiple inputs in the same regex to capture the number following.
If the input doesnt match the first group then I have issues because all previous capture groups are still captured despite it doesnt match they group. I believe I need to use a lookaround assertion, but I am not familiar with those.

Sample input:

wordA 123456
wordA: 123456
wordA : 123456
wordA R123465
wordA: R123456
wordA : R123456
wordB R123465

WordA has both optional : and R. So far I have this: /(?:wordA :?R?(\d+)|wordB R(\d+))/i.
Using the last sample input provides this result:

array
  0 => string 'wordB R123456' (length=13)
  1 => string '' (length=0)
  2 => string '123456' (length=6)

Wanted result is:

array
  0 => string 'wordB R123465' (length=13)
  1 => string '123456' (length=6)

Any ideas how to fix ?

The problem is your alternation

/(?:wordA :?R?(\d+)|wordB R(\d+))/i
              ^^^^^        ^^^^^
             Group 1       Group 2

So if your regex matches the second alternative, the result will be in group 2 (array[2]) and the first group will be empty.

Change it to this

(?:wordA :?R?|wordB R)(\d+)

See it here on Regexr

Then your number will always be in the first group (because there is only one)

You are defining two capturing groups, therefore you'll get two as a result. The regex implementation does not care if only one of them can match at a time. You could rewrite your expression to

/(?:wordA :?R?|wordB R)(\d+)/i

to avoid defining two capturing groups. Note that :?R? is not sufficient to match your test cases with _:_ or :_. To get those, you could use

/(?:wordA ?:? R?|wordB R)(\d+)/i