3

I need to align formatting of some clinical trial IDs two merge two databases. For example, in database A patient 123 visit 1 is stored as '123v01' and in database B just '123v1'

I can match A to B by grep match those containing 'v0' and strip out the trailing zero to just 'v', but for academic interest & expanding R / regex skills, I want to reverse match B to A by matching only those containing 'v' followed by only 1 digit, so I can then separately pad that digit with a leading zero.

For a reprex:

string <- c("123v1", "123v01", "123v001")

I can match those with >= 2 digits following a 'v', then inverse subset

> idx <- grepl("v(\\d{2})", string)
> string[!idx]
[1] "123v1"

But there must be a way to match 'v' followed by just a single digit only? I have tried the lookarounds

# Negative look ahead "v not followed by 2+ digits"
grepl("v(?!\\d{2})", string)

# Positive look behind "single digit following v"
grepl("(?<=v)\\d{1})", string)

But both return an 'invalid regex' error

Any suggestions?

4
  • I'm not much good at regex, but I suggest [vV][0-9]{1}[!0-9] Aug 19, 2019 at 15:08
  • 2
    what about grepl("v\\d{1}$", string)?
    – emilliman5
    Aug 19, 2019 at 15:28
  • Or even shorter, as \\d is one digit: grepl("v\\d$", string), where $ indicates end of string. But maybe its better to remove all leading zeros e.g. with sub("v0*", "v", string) and then make the match.
    – GKi
    Aug 19, 2019 at 16:19
  • Mind that v(?!\d{2}) matches vWORD_HERE - i.e. even when no digit is there after v. See my answer with the proper solution. Aug 19, 2019 at 19:35

2 Answers 2

3

You need to set the perl=TRUE flag on your grepl function.

e.g.

grepl("v(?!\\d{2})", string, perl=TRUE)
[1]  TRUE FALSE FALSE

See this question for more info.

2
  • v(?!\d{2}) matches vWORD_HERE - i.e. even when no digit is there after v. See your regex demo with an unexpected match. Aug 21, 2019 at 7:14
  • Ah thanks, I didn't check whether the OP's regex's solved their problem, I just addressed the "invalid regex" error the OP mentioned.
    – meenaparam
    Aug 21, 2019 at 10:50
1

You may use

grepl("v\\d(?!\\d)", string, perl=TRUE)

The v\d(?!\d) pattern matches v, 1 digits and then makes sure there is no digit immediately to the right of the current location (i.e. after the v + 1 digit).

See the regex demo.

Note that you need to enable PCRE regex flavor with the perl=TRUE argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.