简体   繁体   English

正则表达式删除特定字符

[英]Regex to strip specific characters

I have been using the following regex to replace all punctuation in a string: 我一直在使用以下正则表达式替换字符串中的所有标点符号:

preg_replace('/[^\w\s]/', '', $tweet);

with \\w being shorthand for [a-zA-Z0-9_] and \\s is used to ommit spaces. \\ w是[a-zA-Z0-9_]的简写,而\\ s用于省略空格。 I learned this wisdom here: Strip punctuation in an address field in PHP . 我在这里学到了这种智慧: 在PHP的地址字段中删除标点符号 But now, I need the regex to strip all characters except 但是现在,我需要使用正则表达式来去除除

a-z and A-Z
{ and }

So it should strip out all dots, commas, numbers etc. What is the correct regex for this? 因此,它应该去除所有点,逗号,数字等。对此正确的正则表达式是什么?

preg_replace('/[^a-zA-Z{} ]/', '', $tweet);

FakeRainBrigand在评论中提出的可能更快的变体,谢谢:

preg_replace('/[^a-zA-Z{} ]+/', '', $tweet);
preg_replace('/[^a-z{}]/i', '', $tweet);

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

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