简体   繁体   English

如何在R中构建正则表达式?

[英]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". 我只想选择与模式匹配的列:“ aa [数字] .1”。 In other words to extract only the columns that end with ".1". 换句话说,仅提取以“ .1”结尾的列。

I have tried to use regular expression pattern for grep and dplyr select/filter + ends_with() with pattern = ".1" 我尝试对grep和dplyr select / filter + ends_with()使用正则表达式模式,其模式=“ .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 ( $ ). 您想使用一个正则表达式,该表达式与一个点( \\. )匹配,后跟1然后是字符串结尾( $ )。

Use the following regex then: 然后使用以下正则表达式:

"\\.1$"

The dot must be escaped to match a literal dot. 点必须转义以匹配文字点。

See the regex demo . 参见regex演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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