Find a number after a specific word with grepl and regex {r}

Asked

Viewed 146 times

1

Hello, I have a list of addresses and I’m trying to verify which ones have numbers and which don’t. However, I have some strings that end with number and I’m trying to create a regex to filter these results. I don’t want you to return numbers that come after the words "street" and "km".

> #exemplo:
> enderecos <- c("rua 5", "rua x, casa y", "km 18")
>
> #resultado esperado:
> FALSE TRUE FALSE

I appreciate anyone who can help me.

2 answers

1

The following regular expression should solve the problem:

"(?:km|rua)+\s*\D+?"

Despite the small data show, I believe that if there are more variations just edit them.

Example in Regex101

  • Hello Carlos, this expression is returning me the error Error: ' s' is an unrecognized escape in Character string Starting ""(?:km|street)+ s"

  • 1

    I figured out how to fix it, I need one before S and D :)

1


You can use the following regular expression pattern

(?:km|rua)\s\d{1,9}

I have tested with the informed data and others as well, seems to be sufficient. Follows the test link

  • William, this expression is returning me the following error: Error: ' s' is an unrecognized escape in Character string Starting ""(?:km|street)+ s"

  • 1

    Perhaps the language you are using regards '' as a special character. try to use " " or find out the correct way to insert the bar in the language you are using

  • Yes, that’s right. With (?:km|street) s d{1.9} it worked

  • Great. I’m happy to help. Don’t forget to mark the correct answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.