Remove all leading string before two specific letters in R

Gnin

I am looking for a way to remove all leading strings before two specific letters "bd" and "ls".

However, I only found regex ways to remove string before white space or punctuation. Are there any ways to remove leading string before pairs of specific letters?

      date_on                          location
14 2021-02-22 bradford, west yorkshire, bd9 6dp
15 2021-02-22                     bradford, bd4
16 2021-02-22          bradford, west yorkshire
17 2021-02-22           west yorkshire, bd1 1nq
18 2021-02-22          bradford, west yorkshire
19 2021-02-22                          ls28 7he

dput:

structure(list(date_on = structure(c(18680, 18680, 18680, 18680, 
18680, 18680), class = "Date"), location = c("bradford, west yorkshire, bd9 6dp", 
"bradford, bd4", "bradford, west yorkshire", "west yorkshire, bd1 1nq", 
"bradford, west yorkshire", "ls28 7he")), row.names = 14:19, class = "data.frame")

expected outcome:

      date_on location
14 2021-02-22  bd9 6dp
15 2021-02-22      bd4
16 2021-02-22         
17 2021-02-22  bd1 1nq
18 2021-02-22         
19 2021-02-22 ls28 7he
structure(list(date_on = structure(c(18680, 18680, 18680, 18680, 
18680, 18680), class = "Date"), location = c("bd9 6dp", 
"bd4", "", "bd1 1nq", "", "ls28 7he")), row.names = 14:19, class = "data.frame")
Tim Biegeleisen

We could try using sub here, for a base R option:

df$location <- sub("^.*?(\\b(?:bd|ls)\\d+.*|$)$", "\\1", df$location)
df

      date_on location
14 2021-02-22  bd9 6dp
15 2021-02-22      bd4
16 2021-02-22         
17 2021-02-22  bd1 1nq
18 2021-02-22         
19 2021-02-22 ls28 7he

Here is an explanation of the regex pattern used:

^                     from the start of the location
    .*?               consume all content up to, but not including
    (                 start capture group
        \\b(?:bd|ls)  a postal code starting in 'bd' or 'ls'
        \\d+          followed by one or more digits
        .*            consume the remainder of the location
        |             OR
        $             consume the remainder of any location NOT
                      having at least one postal code
    )                 stop capture group
$                     end of the location

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove all leading string before two specific letters in R

From Dev

string remove all letters

From Dev

How to remove all characters from a string before a specific character

From Dev

How to remove all leading slashes in a string?

From Dev

Remove specific words or letters from a String

From Dev

Use gsub remove all string before first white space in R

From Dev

Remove leading backslash from string R

From Dev

Remove all letters from a string column in Rails

From Dev

How to remove all letters between characters ( and ) in a string?

From Dev

How to remove all letters from string

From Dev

Remove the letters between two patterns of strings in R

From Dev

POSTGRESQL: remove all characters before a specific character

From Dev

Remove All Character before a String and the String itself

From Dev

Remove All Character before a String and the String itself

From Dev

Remove letters matching pattern before and after the required string

From Java

How should I remove all the leading spaces from a string? - swift

From Dev

Javascript remove all leading and trailing junk characters in a string

From Dev

Javascript remove all leading and trailing junk characters in a string

From Dev

Remove all leading zeroes in a string for first column only

From Dev

Add space between two letters in a string in R

From Dev

Remove leading string in bash

From Dev

Remove characters in string before specific symbol(including it)

From Dev

return all the letters that occur before the first number in a string

From Java

Is there a way to use Regex to capture numbers out of a string based on a specific leading letters?

From Dev

How to match two strings before a specific string

From Dev

R: Remove leading zeroes from the beginning of a character string

From Dev

Dataframe split before a specific string for all rows

From Dev

Dataframe split before a specific string for all rows

From Dev

regex to remove last two letters in a number and word string

Related Related

  1. 1

    Remove all leading string before two specific letters in R

  2. 2

    string remove all letters

  3. 3

    How to remove all characters from a string before a specific character

  4. 4

    How to remove all leading slashes in a string?

  5. 5

    Remove specific words or letters from a String

  6. 6

    Use gsub remove all string before first white space in R

  7. 7

    Remove leading backslash from string R

  8. 8

    Remove all letters from a string column in Rails

  9. 9

    How to remove all letters between characters ( and ) in a string?

  10. 10

    How to remove all letters from string

  11. 11

    Remove the letters between two patterns of strings in R

  12. 12

    POSTGRESQL: remove all characters before a specific character

  13. 13

    Remove All Character before a String and the String itself

  14. 14

    Remove All Character before a String and the String itself

  15. 15

    Remove letters matching pattern before and after the required string

  16. 16

    How should I remove all the leading spaces from a string? - swift

  17. 17

    Javascript remove all leading and trailing junk characters in a string

  18. 18

    Javascript remove all leading and trailing junk characters in a string

  19. 19

    Remove all leading zeroes in a string for first column only

  20. 20

    Add space between two letters in a string in R

  21. 21

    Remove leading string in bash

  22. 22

    Remove characters in string before specific symbol(including it)

  23. 23

    return all the letters that occur before the first number in a string

  24. 24

    Is there a way to use Regex to capture numbers out of a string based on a specific leading letters?

  25. 25

    How to match two strings before a specific string

  26. 26

    R: Remove leading zeroes from the beginning of a character string

  27. 27

    Dataframe split before a specific string for all rows

  28. 28

    Dataframe split before a specific string for all rows

  29. 29

    regex to remove last two letters in a number and word string

HotTag

Archive