rregexlogical-operatorsor-condition

Regex or condition for text in r


I have a text suppose

1) "Project:ABC is located near CBA, being too far from city  "
2) "P r o j e c t : PQR is located near RQP, highlights some greenary"

I want to extract text between the word "project" and "," so that my output is"ABC is located near CBA" from text1 and "PQR is located near RQP" from text2, for that I used regex

x="Project:ABC is located near CBA, being too far from city  "
sub(".*Project: *(.*?) *, .*", "\\1", x)
O\P
ABC is located near CBA

But for text2) it doesn't gives proper output so how do I include OR condition so that my both condition is satisfied. Any suggestion will be helpful. Thanks


Solution

  • Make your regex a bit more flexible: [^:]+:\s*([^,]+),.*

    > sub("[^:]+:\\s*([^,]+),.*", "\\1", "P r o j e c t : PQR is located near RQP, highlights some greenary")
    [1] "PQR is located near RQP"
    

    and

    > sub("[^:]+:\\s*([^,]+),.*", "\\1", "Project:ABC is located near CBA, being too far from city  ")
    [1] "ABC is located near CBA"