简体   繁体   English

正则表达式删除连续字符

[英]Regex to remove consecutive characters

Need a regular expression to replace the following sequence 需要一个正则表达式来替换以下序列

Before : abbbccdd   After : abcd

And also if numeric data is present instead of alphabets i would like to remove the duplicates and display 而且如果存在数字数据而不是字母,我也想删除重复项并显示

For the first part, in most languages you can do something like replacing (.)\\1+ with $1 . 对于第一部分,在大多数语言中,您都可以执行类似将(.)\\1+替换$1

The exact syntax depends on the language and the regular expression engine you are using, so check the manual for your language for more details. 确切的语法取决于所使用的语言和正则表达式引擎,因此,请查看所用语言的手册以获取更多详细信息。

This works in PHP: 这适用于PHP:

preg_replace('/(.)\1+/','$1',$str);

I'm not sure what you mean with your second question, though. 不过,我不确定您对第二个问题的意思。

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

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