I would like to have a RegEx that matches several of the same character in a row, within a range of possible characters but does not return those pattern matches as one pattern. How can this be accomplished?

我希望有一个RegEx,它可以匹配行中的几个相同字符,在可能的字符范围内,但不返回这些模式匹配为一个模式。这是如何实现的呢?

For clarification:

澄清:

I want a pattern that starts with [a-c] and ungreedly returns any number of the same character, but not the other characters in the range. In the sequence 'aafaabbybcccc' it would find patterns for:

我想要一个以[a-c]开头的模式,并且不带灰色地返回任何数量的相同字符,但不返回范围中的其他字符。在“aafaabbybcccc”序列中,它会找到以下模式:

('aa', 'aa', 'bb', 'b', 'cccc')

(“aa”、“aa”、“bb”、“b”、“cccc”)

but would exclude the following:

但将排除下列因素:

('f', 'aabb', 'y', 'bcccc')

(' f ',' aabb ',' y ','主教团')

I don't want to use multiple RegEx pattern searches because the order that i find the patterns will determine the output of another function. This question is for the purposes of self study (python), not homework. (I'm also under 15 rep but will come back and upvote when I can.)

我不想使用多个RegEx模式搜索,因为我找到模式的顺序将决定另一个函数的输出。这个问题是为了自学(python),而不是家庭作业。(我也在15岁以下,但如果可以的话,我会回来投票。)

2 个解决方案

#1


2

Good question. Use a regex like:

好问题。使用一个正则表达式:

(?P<L>[a-c])(?P=L)+

This is more robust - you're not limited to a-c, you can replace it with a-z if you like. It first defines any character within a-c as L, then sees whether that character occurs again one or more times. You want to run re.findall() using this regex.

这更健壮——你不局限于a-c,你可以用a-z替换它。它首先将a-c中的任何字符定义为L,然后查看该字符是否再次出现一次或多次。您希望使用此regex运行re.findall()。

更多相关文章

  1. Emacs 快速参考 c-c++ 模式
  2. linux shell脚本编程笔记(四): 获取字符串长度的七种方法
  3. 计算所有字符,包括linux中的空格
  4. 如何使用sed删除特殊字符
  5. Linux Shell编程(15)——操作字符串
  6. linux中常用时间和字符串之间相互转化
  7. Bash脚本删除目录中多个文件名末尾的'x'字符数量?
  8. 文本文件到字符串数组?
  9. 字符串处理函数strcat和strtok

随机推荐

  1. 键入提示 - 指定对象数组
  2. PHP MySqli Multiple query & while in o
  3. MySQL命名约定,字段名称是否应包含表名?
  4. composer不能生成sf2 autoload文件。
  5. 用一个听众听多个事件?
  6. PHP常用函数归类总结【大全】
  7. 使用php浏览文件时,编写文件的完整路径
  8. Windows 环境下php安装openssl证书
  9. CentOS下安装php 5.6.19
  10. php项目防止sql注入的方法