Регулярное выражение, если затем шаблон возвращает нулевой символ

#r #regex #conditional-statements

Вопрос:

Используя шаблон:

 (?((?<=(Textbook|Online Class): (Yes|No))(?!(Textbook|Online Class): (Yes|No))). |(?<=(\d{4}))(?!(U000)). ) 
 

Дает мне:

 Error: null character not allowed
 

Я хотел, чтобы шаблон различал, присутствует ли учебник/Онлайн: Да/Нет, а за ним не следует Учебник/Онлайн: Да/Нет, затем захватите остальное, иначе найдите четырехзначное число, за которым не следует, "U000" и захватите остальное.

Чтобы быть более конкретным, когда шаблон применяется к строкам

 "ECP3203U0001f60eawesomeJan 5th, 2005Some interesting stuff in the class.  Do the work and go to class and pay attention and its easy.  The paper is easier than it seems.00" 
 

и

 "ECP3203U0001f60eawesomeDec 21st, 2010Textbook: NoEasy A.  Go to class and write down whatever she writes on the board and you will ace the tests.  Grading scheme also makes it easy to get an A.  I'd recommend taking this course.  The only problem is that she sometimes let's class discussions go off topic.00"
 

Он должен вернуться

 "Some interesting stuff in the class. Do the work and go to class and pay attention and its easy. The paper is easier than it seems.00" 
 

и

 "Easy A. Go to class and write down whatever she writes on the board and you will be the tests. Grading scheme also makes it easy to get an A. I'd recommend taking this course. The only problem is that she sometimes let's class discussions go off topic.00"
 

Комментарии:

1. Пожалуйста, предоставьте полный код, который позволил бы любому другому воспроизвести то, что у вас есть, скопировав и вставив в R.

2. Попробуй sub("^.*?\b\d{4}(?:(?:Textbook|Online Class):\s*(?:Yes|No))?", "", x, perl=TRUE)

3. Пожалуйста, просмотрите правку

Ответ №1:

Вы можете попробовать использовать следующее регулярное выражение —

 sub('.*\d{4}(Textbook: (Yes|No))?', '', x)

#[1] "Some interesting stuff in the class.  Do the work and go to class and pay attention and its easy.  The paper is easier than it seems.00"                                                                                                                            

#[2] "Easy A.  Go to class and write down whatever she writes on the board and you will ace the tests.  Grading scheme also makes it easy to get an A.  I'd recommend taking this course.  The only problem is that she sometimes let's class discussions go off topic.00" 
 

*данные

 x <- c("ECP3203U0001f60eawesomeJan 5th, 2005Some interesting stuff in the class.  Do the work and go to class and pay attention and its easy.  The paper is easier than it seems.00", 
       "ECP3203U0001f60eawesomeDec 21st, 2010Textbook: NoEasy A.  Go to class and write down whatever she writes on the board and you will ace the tests.  Grading scheme also makes it easy to get an A.  I'd recommend taking this course.  The only problem is that she sometimes let's class discussions go off topic.00")