项目作者: alverelt

项目描述 :
Experimental regular expression builder with chainable methods.
高级语言: Python
项目地址: git://github.com/alverelt/meaningful_re.git
创建时间: 2020-07-07T04:54:39Z
项目社区:https://github.com/alverelt/meaningful_re

开源协议:MIT License

下载


meaningful_re

Experimental regular expression builder with chainable methods.

Instead of having to read and figure out the meaning of those weird strings, you can use an object to build your regex with methods with more sense.

Example

Let’s say you want to make a regular expression like r'^[hc]at$', you can do it like this:

  1. from meaningful_re import MeaningfulRE as MRE
  2. mre = (MRE('[hc]')
  3. .concat('at')
  4. .match_start
  5. .match_end)
  6. print mre.regex

Note: It doesn’t matter if you use match_start or match_end at the beginning, middle or end of you chaining methods, when you show the regex they will be in their corresponding string position.

Or if you want to make an email regular expression like r'\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b', not everybody can read that, but, with meaningful_re it makes more sense doing it.

  1. from meaningful_re import MeaningfulRE as MRE
  2. mre = (MRE()
  3. .word_boundary
  4. .any_of(MRE.RANGE_UPPERCASE_ALPHABET, MRE.RANGE_NUMBERS, '._%+-')
  5. .concat('+@')
  6. .any_of(MRE.RANGE_UPPERCASE_ALPHABET, MRE.RANGE_NUMBERS, '.-')
  7. .concat('+\.')
  8. .any_of(MRE.RANGE_UPPERCASE_ALPHABET).at_least(2)
  9. .word_boundary)

Another example using capturing group, if you would like to match all string starting with IMG and ending with either .png or jpeg like r'^(IMG\d+)\.(png|jpeg)$', can be built this way:

  1. from meaningful_re import MeaningfulRE as MRE
  2. mre = (MRE()
  3. .match_start
  4. .gi
  5. .one_or_more('IMG' + MRE.DIGIT)
  6. .ge
  7. .concat('\.')
  8. .gi
  9. .or_('png', 'jpeg')
  10. .ge)