Elegant way to use regex to match order-indifferent groups of characters (in a substring) while limiting how may times a given character can appear?

Toaster

This is a follow-on to an earlier question. This question amends the question to the original intent and increases complexity by adding the requirement that the regex work with substrings.

I am looking for a way to use python regular expressions to match groups of characters with limits on how many times a character can appear in the match. The main problem is that the order of characters does not matter.

I would like to find a simple and extensible pattern for saying things like:

  • Find 4 characters together.
  • All of the characters must be from the group 'ABCD'
  • 0 to 4 of them can be 'A'
  • 0 to 3 of them can be 'B'
  • 0 to 3 of them can be 'C'
  • 0 to 1 of them can be 'D'

In which case the following substrings match:

AAAA AAAB AAAC AAAD AABA AABB AABC AABD AACA AACB AACC AACD AADA AADB AADC ABAA ABAB ABAC ABAD ABBA ABBB ABBC ABBD...

and the following substrings would not match:

CCCC DDDD DDDA DDAA DDAB ...

I put the match list here.

Is there a pattern for this type of match that doesn't involve listing all possible combinations?

alpha bravo

use this pattern to match your criteria as a sub-string

(?!B{4})(?!C{4})(?!([ABC]?D){2})[A-D]{4}  
(?!B{4})            # does not see 4B's
(?!C{4})            # does not see 4C's
(?!([ABC]?D){2})    # does not see 2D's in any order
[A-D]{4}            # [A-D] 4 times

Edit:
After reading comments below, give this pattern a try:

(?!B{4})(?!C{4})((?!.?.?DD|.?D.D|D..D)[A-D]{4})

Demo

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

regexp to match characters not flowed by a character that is repeated an amount of times

来自分类Dev

Regex multiple match substring

来自分类Dev

Regex: How to match sequence of SAME characters?

来自分类Dev

How to dynamically create regex to use in .match Javascript?

来自分类Dev

Regex to match and limit character classes

来自分类Dev

Limiting groups with a query

来自分类Dev

Regex - Match all numbers until character

来自分类Dev

Regex match but exclude character from capture group

来自分类Dev

Can not catch substring by regex which ends with tab

来自分类Dev

How to match specific characters only?

来自分类Dev

Python: how to search for a substring in a set the fast way?

来自分类Dev

How to insert a newline character to an array of characters

来自分类Dev

Lua: How to start match after a character

来自分类Dev

How to match every A's and B's but not a certain substring

来自分类Dev

Regex pattern to find n non-space characters of x length after a certain substring

来自分类Dev

Count characters and substring in words

来自分类Dev

Is there a way to use a WHILE and IF/ELSE statement in SQL?

来自分类Dev

Is there a way to use a WHILE and IF/ELSE statement in SQL?

来自分类Dev

How to get regex match on my scenario

来自分类Dev

Java Regex: How to Match URL Path?

来自分类Dev

How to remove all characters from a string before a specific character

来自分类Dev

Search for permutation of characters of a substring in Python

来自分类Dev

faster way to agrep? Quickly find every single character mis-match

来自分类Dev

How can I define an UUID for a class, and use __uuidof, in the same way for g++ and Visual C++?

来自分类Dev

Elegant way to compare to torch.FloatTensor on GPU

来自分类Dev

Elegant way to serialize a MailMessage object in .NET

来自分类Dev

How can I simply consume unrecognized characters?

来自分类Dev

How to remove duplicate lines while keeping the order AND ignoring case?

来自分类Dev

Working with Named Regex Groups in Ruby

Related 相关文章

  1. 1

    regexp to match characters not flowed by a character that is repeated an amount of times

  2. 2

    Regex multiple match substring

  3. 3

    Regex: How to match sequence of SAME characters?

  4. 4

    How to dynamically create regex to use in .match Javascript?

  5. 5

    Regex to match and limit character classes

  6. 6

    Limiting groups with a query

  7. 7

    Regex - Match all numbers until character

  8. 8

    Regex match but exclude character from capture group

  9. 9

    Can not catch substring by regex which ends with tab

  10. 10

    How to match specific characters only?

  11. 11

    Python: how to search for a substring in a set the fast way?

  12. 12

    How to insert a newline character to an array of characters

  13. 13

    Lua: How to start match after a character

  14. 14

    How to match every A's and B's but not a certain substring

  15. 15

    Regex pattern to find n non-space characters of x length after a certain substring

  16. 16

    Count characters and substring in words

  17. 17

    Is there a way to use a WHILE and IF/ELSE statement in SQL?

  18. 18

    Is there a way to use a WHILE and IF/ELSE statement in SQL?

  19. 19

    How to get regex match on my scenario

  20. 20

    Java Regex: How to Match URL Path?

  21. 21

    How to remove all characters from a string before a specific character

  22. 22

    Search for permutation of characters of a substring in Python

  23. 23

    faster way to agrep? Quickly find every single character mis-match

  24. 24

    How can I define an UUID for a class, and use __uuidof, in the same way for g++ and Visual C++?

  25. 25

    Elegant way to compare to torch.FloatTensor on GPU

  26. 26

    Elegant way to serialize a MailMessage object in .NET

  27. 27

    How can I simply consume unrecognized characters?

  28. 28

    How to remove duplicate lines while keeping the order AND ignoring case?

  29. 29

    Working with Named Regex Groups in Ruby

热门标签

归档