求教一个简单的regex问题

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

Stia
Posts: 1
Joined: 09 Jul 2020, 20:26

求教一个简单的regex问题

Post by Stia » 09 Jul 2020, 20:54

有如下R代码,我希望把注释去掉,把代码行提取出来(第四行):
# this is comment 1
# this is comment 2
# this is comment n
variable <- 123
尝试了如下regex,好像还是不行。求助高手!

Code: Select all

^(?<!#)(.+?)$
Attachments
1111.png
测试结果
1111.png (45.48 KiB) Viewed 6069 times

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 求教一个简单的regex问题

Post by tmplinshi » 10 Jul 2020, 20:34

Code: Select all

str =
(
# this is comment 1
# this is comment 2
# this is comment n
variable <- 123
)

RegExMatch(str, "`am)^(?!#).+$", match)
MsgBox, % match

RegExMatch(str, "`am)^[^#]+$", match)
MsgBox, % match
`a 识别任何形式的换行, 比如 `r, `n, `r`n
m 表示只匹配单行

Guest

Re: 求教一个简单的regex问题

Post by Guest » 11 Jul 2020, 23:17

多谢!!
tmplinshi wrote:
10 Jul 2020, 20:34

Code: Select all

str =
(
# this is comment 1
# this is comment 2
# this is comment n
variable <- 123
)

RegExMatch(str, "`am)^(?!#).+$", match)
MsgBox, % match

RegExMatch(str, "`am)^[^#]+$", match)
MsgBox, % match
`a 识别任何形式的换行, 比如 `r, `n, `r`n
m 表示只匹配单行

Post Reply

Return to “请求帮助”