Extract all text before the last occurrence of a specific word

Viewed 61

I wanted to extract all the text before the last occurrence of a specific word:

Please note again that and-approved forms are not to be count toward the page limit. To the extent possible, please limit the appendix file to one 50-page PDF submission.

The Project Description must be clear, concise, and complete. is particularly interested in Project Descriptions that convey strategies for achieving intended performance. Project Descriptions are evaluated on the basis of substance and measurable outcomes, not length. Cross-referencing should be used rather than repetition. Supporting documents designated as required must be included in the Appendix of the document

Clearly identify the physical, economic, social, financial, institutional, and/or other problem(s) requiring a solution. The need for assistance, including the nature and scope of the problem, must be demonstrated. Supporting documentation, such as letters of support and testimonials from concerned parties, may be included in the Appendix. Any relevant data based on planning studies or needs assessments should be included or referred to in the endnotes or footnotes. Incorporate demographic data and participant/beneficiary information, as available.

Appendix

Any relevant data based on planning studies or needs assessments should be included or referred to in the endnotes or footnotes. Incorporate demographic data and participant/beneficiary information, as available.

And I want to all the text before the last Appendix (excluding Appendix)

currently using this

sub("(.*)Appendix", "", text)

but it only get all the text in the paragraph with the first Appendix. How do I adjust the regex.

Expected output:

Please note again that and-approved forms are not to be count toward the page limit. To the extent possible, please limit the appendix file to one 50-page PDF submission.

The Project Description must be clear, concise, and complete. is particularly interested in Project Descriptions that convey strategies for achieving intended performance. Project Descriptions are evaluated on the basis of substance and measurable outcomes, not length. Cross-referencing should be used rather than repetition. Supporting documents designated as required must be included in the Appendix of the document

Clearly identify the physical, economic, social, financial, institutional, and/or other problem(s) requiring a solution. The need for assistance, including the nature and scope of the problem, must be demonstrated. Supporting documentation, such as letters of support and testimonials from concerned parties, may be included in the Appendix. Any relevant data based on planning studies or needs assessments should be included or referred to in the endnotes or footnotes. Incorporate demographic data and participant/beneficiary information, as available.

4 Answers

You can use the following code to select everything before the last occurence of appendix:

sub('-[^Appendix]*$', '', text)

Output:

[1] "Please note again that and-approved forms are not to be count toward the page limit. To the extent possible, please limit the appendix file to one 50-page PDF submission.\n\nThe Project Description must be clear, concise, and complete. is particularly interested in Project Descriptions that convey strategies for achieving intended performance. Project Descriptions are evaluated on the basis of substance and measurable outcomes, not length. Cross-referencing should be used rather than repetition. Supporting documents designated as required must be included in the Appendix of the document\n\nClearly identify the physical, economic, social, financial, institutional, and/or other problem(s) requiring a solution. The need for assistance, including the nature and scope of the problem, must be demonstrated. Supporting documentation, such as letters of support and testimonials from concerned parties, may be included in the Appendix. Any relevant data based on planning studies or needs assessments should be included or referred to in the endnotes or footnotes. Incorporate demographic data and participant/beneficiary information, as available.\n\nAppendix\n\nAny relevant data based on planning studies or needs assessments should be included or referred to in the endnotes or footnotes. Incorporate demographic data and participant/beneficiary information, as available."

We can find the location of 'Appendix', extract the last (dplyr) 'start'ing index, and get the substring (str_sub) from the start (1) to that location (wrap with trimws to remove any leading/lagging spaces)

library(stringr)
out <- trimws( str_sub(text, 1, dplyr::last(str_locate_all(text, 
    "Appendix")[[1]][,1])-1))

-output

> cat(out, "\n")
Please note again that and-approved forms are not to be count toward the page limit. To the extent possible, please limit the appendix file to one 50-page PDF submission.

    The Project Description must be clear, concise, and complete. is particularly interested in Project Descriptions that convey strategies for achieving intended performance. Project Descriptions are evaluated on the basis of substance and measurable outcomes, not length. Cross-referencing should be used rather than repetition. Supporting documents designated as required must be included in the Appendix of the document

    Clearly identify the physical, economic, social, financial, institutional, and/or other problem(s) requiring a solution. The need for assistance, including the nature and scope of the problem, must be demonstrated. Supporting documentation, such as letters of support and testimonials from concerned parties, may be included in the Appendix. Any relevant data based on planning studies or needs assessments should be included or referred to in the endnotes or footnotes. Incorporate demographic data and participant/beneficiary information, as available. 

data

text <- "\n\n    Please note again that and-approved forms are not to be count toward the page limit. To the extent possible, please limit the appendix file to one 50-page PDF submission.\n\n    The Project Description must be clear, concise, and complete. is particularly interested in Project Descriptions that convey strategies for achieving intended performance. Project Descriptions are evaluated on the basis of substance and measurable outcomes, not length. Cross-referencing should be used rather than repetition. Supporting documents designated as required must be included in the Appendix of the document\n\n    Clearly identify the physical, economic, social, financial, institutional, and/or other problem(s) requiring a solution. The need for assistance, including the nature and scope of the problem, must be demonstrated. Supporting documentation, such as letters of support and testimonials from concerned parties, may be included in the Appendix. Any relevant data based on planning studies or needs assessments should be included or referred to in the endnotes or footnotes. Incorporate demographic data and participant/beneficiary information, as available.\n\n    Appendix\n\n    Any relevant data based on planning studies or needs assessments should be included or referred to in the endnotes or footnotes. Incorporate demographic data and participant/beneficiary information, as available.\n"

Here's a generalisable tidyverse solution to your problem. For illustrative purposes let me demonstrate it using a much shorter example string:

Data #1:

text <- "Appendix abc Appendix def Appendix final"

Solution:

(library(tidyverse)
data.frame(text) %>%
  # split `text` into separate words:
  separate_rows(text, sep = " ") %>%
  # count the words depending on whether they contain `Appendix` or `appendix`:
  mutate(count_Appendix = cumsum(str_count(text, "(a|A)ppendix"))) %>%
  # subset on those words that are not the maximum (i.e., last) `count_Appendix`:
  filter(count_Appendix != max(count_Appendix)) %>%
  # put the words back together:
  summarise(text = str_c(text, collapse = " "))
# A tibble: 1 × 1
  text                     
  <chr>                    
1 Appendix abc appendix def

In most cases, you will have more than one text string. Two adaptations to the above code are required: (i) create a row identifier and (ii) group_by that identifier before you paste the words back into their original format:

data.frame(text) %>%
  # create row identifier:
  mutate(row = row_number()) %>%
  # split `text` into separate words:
  separate_rows(text, sep = " ") %>%
  # count the words depending on whether they contain `Appendix` or `appendix`:
  mutate(count_Appendix = cumsum(str_count(text, "(a|A)ppendix"))) %>%
  # subset on those words that are not the maximum (i.e., last) `count_Appendix`:
  filter(count_Appendix != max(count_Appendix)) %>%
  # for each `row`:
  group_by(row) %>%
  # put the words back together:
  summarise(text = str_c(text, collapse = " "))
# A tibble: 2 × 2
    row text                                    
  <int> <chr>                                   
1     1 Appendix abc Appendix def Appendix final
2     2 blah blah Appendix abc  

Data #2:

text <- c("Appendix abc Appendix def Appendix final",
          "blah blah Appendix abc Appendix finalissimo")

EDIT:

See here for regex solutions: Regex to edit text depending on number of occurrence of key word

You can use sub. The first .* is greedy and will take everything until the last match of Appendix.* and can be reinserted with \\1 when placing it between ().

sub("(.*)Appendix.*", "\\1", text)

Output:

[1] "\n\n    Please note again that and-approved forms are not to be count toward the page limit. To the extent possible, please limit the appendix file to one 50-page PDF submission.\n\n    The Project Description must be clear, concise, and complete. is particularly interested in Project Descriptions that convey strategies for achieving intended performance. Project Descriptions are evaluated on the basis of substance and measurable outcomes, not length. Cross-referencing should be used rather than repetition. Supporting documents designated as required must be included in the Appendix of the document\n\n    Clearly identify the physical, economic, social, financial, institutional, and/or other problem(s) requiring a solution. The need for assistance, including the nature and scope of the problem, must be demonstrated. Supporting documentation, such as letters of support and testimonials from concerned parties, may be included in the Appendix. Any relevant data based on planning studies or needs assessments should be included or referred to in the endnotes or footnotes. Incorporate demographic data and participant/beneficiary information, as available.\n\n    "
Related