优雅的正则表达式匹配所有标点符号但不是在的Emacs Lisp“'”?

StackOverflow https://stackoverflow.com/questions/1894802

  •  19-09-2019
  •  | 
  •  

我想匹配所有标点符号,而不是“'”,如“I'm”。例如,在下面的句子:

I'm a student, but I'm also working. 
 ^not match  ^match ^not           ^match

我可以使用“[[:punct:]]+”匹配所有标点符号,但是我有很难从匹配图案排除“'”。

当然,我可以使用像成才下面列举通过表达,但它的很多繁琐的,特别是考虑到对中国所有的标点为好。 “[,.?!]

请提供一个更好的解决方案。

由于提前,

有帮助吗?

解决方案 2

感谢Bart的答案,所有的评论。由Bart的启发,我检查了Emacs是仍然不支持先行呢。但在精神,我编码以下内容:

(defun定义字符串匹配但 - 排除(正则表达式串排阻&可选的起始)

“返回正规表达式第一匹配的开始的索引中的字符串,或零, 但不包括正规快件在排斥。 匹配忽略情况下,如果case-fold-search' is non-nil. If third arg start is non-nil, start search at that index in string. For index of first char beyond the match, do (match-end 0). match端“和'比赛开始”也给子的指标 由括号构建体的图案匹配。

可以使用函数`匹配字串”来提取子 通过在正则表达式的括号的结构匹配的“。

(让((数据无))

(and (string-match regexp string start)

   ;; keep the match-data for recovery at the end. 

   (setq data (match-data))

   (not (string-match (concat "[" exclusion "]") (match-string 0 string)))

   (progn (set-match-data data) t) ; To recover the match data, and make sure it produces t as returned value

   (match-beginning 0)

   ))

因此,对于相当于表达 '[[:PUNCT:]]串(!)“'“)

这将是

(字符串匹配但 - 排除 “[[:PUNCT:]]” 串 “'”)

这将做的工作,但并不优雅。这应该是一个次要除了emacs的,使这个内置的支持。

的emacs现在确实支持字符类。

再次感谢。

其他提示

如果您正则表达式的味道支持查找变通,你可以这样做:

(?!')[[:punct:]]

在简单的英语:如果展望的时候有没有单引号,匹配任何标点符号

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top