Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jcarbaugh/tyrannosaurusregex
https://github.com/jcarbaugh/tyrannosaurusregex
Last synced: 22 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/jcarbaugh/tyrannosaurusregex
- Owner: jcarbaugh
- License: bsd-3-clause
- Created: 2013-02-16T00:59:00.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2013-10-26T02:05:30.000Z (about 11 years ago)
- Last Synced: 2024-12-01T09:12:48.731Z (25 days ago)
- Language: Python
- Size: 93.8 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
tyrannosaurusregex
==================Just playing around with some regular expression ideas. Other ideas or suggestions are welcome.
The goal is to make it easy to do simple regex stuff. Not that it's incredibly HARD right now, but it could be easier, right?
::>>> from trex import rex
>>> r = rex(r"([a-z]{3})")
>>> for m in r("abc def GHi jlkm opqr stuvw xYz"):
>>> print m
['abc']
['def']
['jlk']
['opq']
['stu']Or...
::>>> from trex import rex
>>> zipcode = "55555"
>>> rex("\d{5}$").matches(zipcode)
TrueUsage
-----Create your TyrannosaurusRegex object by calling `rex` with your pattern.
::>>> from trex import rex
>>> r = rex(r"(?P[a-z]{3})")You can pass flags as arguments to `rex` or set them on the returned TyrannosaurusRegex object.
::>>> r = rex(r"(?P[a-z]{3})", ignorecase=True)
>>> r.ignorecase = FalseTo find matches, the TyrannosaurusRegex object can be called with the text that is to be searched.
::>>> text = "abc def GHi jlkm opqr stuvw xYz"
>>> g = r(text)
>>> matches = list(g)Calling the TyrannosaurusRegex object returns a generator that emits Matchtodon objects. A Matchtodon is a list of the matched groups.
::>>> print matches
[['abc'], ['def'], ['jlk'], ['opq'], ['stu']]A Matchtodon also provides dict-like access to named groups.
::>>> print matches[0]['threeletters']
abcChanging a flag will force recompilation of the pattern.
::>>> print list(r(text))
[['abc'], ['def'], ['jlk'], ['opq'], ['stu']]
>>> r.ignorecase = True
>>> print list(r(text))
[['abc'], ['def'], ['GHi'], ['jlk'], ['opq'], ['stu'], ['xYz']]Don't do this
-------------I'm not actually proposing this, unless you think it's a good idea... in which case I take full credit for it.
Equality acts like re.match. The objects are equal if zero or more characters at the beginning of the string match the pattern.
::>>> print "ABC" == rex("[a-z]{3}")
False
>>> print "abcdef" == rex("[a-z]{3}")
True
>>> print "abcdef" == rex("[a-z]{3}$")
FalseThe *in* operator acts like re.search. It evaluates to True if the pattern is found anywhere in the string. I'd love to be able to reverse the operands, but oh well.
::>>> print "235 wgg ADGKJE" in rex("[a-z]{3}")
True