关于 r:Regex;消除所有标点符号,除了 | 珊瑚贝

Regex; eliminate all punctuation except


我有以下正则表达式,可以拆分任何空格或标点符号。如何从 :punct: 中排除 1 个或多个标点符号?假设我想排除撇号和逗号。我知道我可以明确地使用 [all punctuation marks in here] 而不是 [[:punct:]] 但我希望有一个排除方法。

1
2
3
4
5
X <-“I’m not that good at regex yet, but am getting better!”
strsplit(X,”[[:space:]]|(?=[[:punct:]])”, perl=TRUE)

 [1]”I”      “‘”      “m”      “not”    “that”   “good”   “at”     “regex”  “yet”    
[10]”,”      “”       “but”    “am”     “getting””better” “!”


我不清楚你想要的结果是什么,但你可以使用像这个答案这样的否定类。

1
2
3
R> strsplit(X,”[[:space:]]|(?=[^,'[:^punct:]])”, perl=TRUE)[[1]]
 [1]”I’m”    “not”    “that”   “good”   “at”     “regex”  “yet,”  
 [8]”but”    “am”     “getting””better” “!”

如果右边的下一个字符是 ‘ 或 ,,您可以直接使用 (?![‘,]) 否定前瞻来对 PCRE 子模式施加限制,这会导致匹配失败:

1
2
[[:space:]]|(?=(?![‘,])[[:punct:]])
               ^^^^^^^^

查看正则表达式演示。

详情

  • [[:space:]] – 任何空格
  • | – 或
  • (?=(?![‘,])[[:punct:]]) – 一个正向预测,要求在当前位置的右侧没有 ‘ 和 , 并且有任何 1 个不是 ‘ 或 , 的标点符号 (实际上,需要除 ‘ 和 , 之外的任何标点符号)。

查看 R 在线演示

1
2
3
4
5
X <-“I’m not that good at regex yet, but am getting better!”
strsplit(X,”[[:space:]]|(?=(?![‘,])[[:punct:]])”, perl=TRUE)
[[1]]
 [1]”I’m”    “not”    “that”   “good”   “at”     “regex”  “yet,”  
 [8]”but”    “am”     “getting””better” “!”

来源:https://www.codenong.com/13372438/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9386.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?