简体   繁体   中英

How can I build regular expression in R?

I have the following dataframe:

aa123 aa123.1 aa456 aa456.1
1     2       3     4

I want to take only the columns that match a pattern: "aa[numbers].1". In other words to extract only the columns that end with ".1".

I have tried to use regular expression pattern for grep and dplyr select/filter + ends_with() with pattern = ".1"

But nothing seems to work, please advise me how can I build such pattern and point me to a guide how in the future to build the pattern I need.

You want to use a regular expression that matches a dot ( \\. ) followed with 1 followed with end of string ( $ ).

Use the following regex then:

"\\.1$"

The dot must be escaped to match a literal dot.

See the regex demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM