正则表达式如何匹配所有标点符号,但排除某些条件 [英] Regex how to match all punctuations but exclude some conditions

查看:82
本文介绍了正则表达式如何匹配所有标点符号,但排除某些条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 \ p {Punct} 来匹配所有标点符号(包括下划线).

I can use \p{Punct} to match all punctuations(including underscore).

我想完全排除一个单词内的所有撇号.为此,我正在使用(?< = [a-zA-Z])'(?= [a-zA-Z])

And I wanted to exclude all apostrophes strictly inside a word. For this I'm using (?<=[a-zA-Z])'(?=[a-zA-Z])

但是我不能让它们一起工作以匹配所有标点符号,但严格把单词内的撇号除外.

However I couldn't have them work together to match all punctuations except apostrophes strictly inside a word.

我应该使用什么?

示例:

我哥哥的应该不匹配.

我兄弟的应该匹配.

我的兄弟" 这些应该匹配.

推荐答案

您可以在此处组合三个条件.

You can combine three conditions here.

  1. 使用 [\ p {Punct}&& [^']]

匹配所有撇号而不是字母.

Match all apostrophe not followed by a letter.

匹配所有不带字母的撇号.

Match all apostrophe not preceded by a letter.

正则表达式: [\ p {Punct}&& [^']] |(?? lt !! [a-zA-Z])'|'(?![a-zA-Z])

说明:

  • [\\ p {Punct}&& [^']] 从标点符号类中排除撇号.

  • [\\p{Punct}&&[^']] excludes apostrophe from punctuation class.

(?<![a-zA-Z])'匹配不带字母的撇号.

(?<![a-zA-Z])' matches apostrophe not preceded by a letter.

'(?![a-zA-Z])匹配撇号而不是字母.

'(?![a-zA-Z]) matches the apostrophe not followed by a letter.

RegexPlanet小提琴

RegexPlanet Fiddle

这篇关于正则表达式如何匹配所有标点符号,但排除某些条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆