简体   繁体   English

正则表达式以提取标点符号/字符

[英]Regular expressions to extract punctuations/characters

Hi guys I need a regex that only extracts punctuations/characters. 嗨,大家好,我需要一个仅提取标点符号/字符的正则表达式。

I have this so far : 到目前为止,我有:

[._^%$#!~@,-]+

but this works if there is at least 1 punctuations and still allows for any other char (digit or letter) 但这在至少有1个标点符号的情况下仍然有效,但仍然允许任何其他字符(数字或字母)

I need to to only allow punctuations/characters 我只需要允许标点符号/字符

Try this: 尝试这个:

^[\p{S}\p{P}]+$

\\p{S} matches any symbol character, and \\p{P} matches any punctuation. \\p{S}匹配任何符号字符, \\p{P}匹配任何标点符号。

Note that your pattern will not match all the symbols and punctuations not present in the list. 请注意,您的模式将与列表中不存在的所有符号和标点符号不匹配。

Try anchoring the regex to the start and the end of the string (unless you're using Multiline matching) - ie ^ at the beginning and $ at the end: 尝试将正则表达式锚定到字符串的开头和结尾(除非您使用的是多行匹配)-即,在开头$ ^ ,在结尾$

^[._^%$#!~@,-]+$

Note - this does not endorse your actual pattern (I can't say whether this is matching all the 'special characters' you're talking about, but it will make it so that the entire string must be all 'special'. 注意-这并不支持您的实际模式(我不能说这是否与您正在谈论的所有“特殊字符”相匹配,但是这样做会使整个字符串必须全部为“特殊”。

[^a-zA-Z0-9]* u can try something like this. [^ a-zA-Z0-9] *您可以尝试类似的方法。 Should NOT accept those chars, cba to writte all beside chars beside one u typed. 不应该接受这些字符,CBA会在您键入的字符旁边写上所有字符。

\W

匹配不是单词字符的任何字符(字母数字和下划线)。

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

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