Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex a before b with and without whitespaces

I am trying to find all the string which include ‘true’ when there is no ‘act’ before it.

An example of possible vector:

vector = c("true","trueact","acttrue","act true","act really true")

What I have so far is this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

grepl(pattern="(?<!act)true", vector, perl=T, ignore.case = T)
[1]  TRUE  TRUE FALSE  TRUE  TRUE

what I’m hopping for is

[1]  TRUE  TRUE FALSE  FALSE  FALSE  

>Solution :

Here is one way to do so:

grepl(pattern="^(.(?<!act))*?true", vector, perl=T, ignore.case = T)
[1]  TRUE  TRUE FALSE FALSE FALSE

  • ^: start of the string
  • .: matches any character
  • (?<=): negative lookbehind
  • act: matches act
  • *?: matches .(?<!act) between 0 and unlimited times
  • true: matches true

see here for the regex demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading