https://github.com/howl-anderson/microregex
一个微型的正则表达式引擎 | A micro regular expression engine
https://github.com/howl-anderson/microregex
finite-state-machine regular-expression regular-expression-engine
Last synced: about 1 year ago
JSON representation
一个微型的正则表达式引擎 | A micro regular expression engine
- Host: GitHub
- URL: https://github.com/howl-anderson/microregex
- Owner: howl-anderson
- License: mit
- Created: 2016-11-29T12:28:01.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-09-22T14:53:56.000Z (almost 7 years ago)
- Last Synced: 2025-06-07T04:47:49.724Z (about 1 year ago)
- Topics: finite-state-machine, regular-expression, regular-expression-engine
- Language: Jupyter Notebook
- Homepage:
- Size: 1.66 MB
- Stars: 36
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.en-US.md
- License: LICENSE.md
Awesome Lists containing this project
README
[中文版本的 README](README.md)
------------------------------
## What's MicroRegEx
MicroRegEx is a micro regular expression engine.
## Operator
* `*` - zero or more repetitions
* `+` - one or more repetitions
* `?` - optional
* `a|b` - matches a or b
* `(expr)` - treat the `expr` as an atom
* `\` - escape character
## Usage
### Use like python built-in regex
```python
import MicroRegEx
regex = MicroRegEx.compile("(a|b)cd*e?")
result = regex.match("abcde")
print(result)
result = regex.match("acde")
print(result)
```
will output:
```text
False
True
```
### Plot NFA
```python
import MicroRegEx
regex = MicroRegEx.compile("(a|b)c?")
regex.plot()
```
will plot graph as fellow:

### Translate to DFA
#### NFA to DFA
##### Native DFA
```python
import MicroRegEx
from MicroRegEx.Automaton.NFA2DFA import NFA2DFA
nfa = MicroRegEx.compile("(a|b)c?")
dfa = NFA2DFA(nfa).convert()
dfa.plot()
```
will plot graph as fellow:

##### Simplified DFA
```python
import MicroRegEx
from MicroRegEx.Automaton.NFA2DFA import NFA2DFA
nfa = MicroRegEx.compile("(a|b)c?")
dfa = NFA2DFA(nfa).convert().simplify()
dfa.plot()
```
will plot graph as fellow:

#### DFA minimization
##### Brzozowski method
```python
import MicroRegEx
from MicroRegEx.Automaton.NFA2DFA import NFA2DFA
from MicroRegEx.Automaton.Minimal.Brzozowski import Brzozowski
nfa = MicroRegEx.compile("(a|b)c?")
dfa = NFA2DFA(nfa).convert().simplify()
mini_dfa = Brzozowski(dfa).construct()
mini_dfa.plot()
```
will plot graph as fellow:

## Understand MicroRegEx
To better understand how MicroRegEx works, I have provided a [Jupyter Notebook](example/explain_code.ipynb) that allows the user to gradually explore the underlying principles of MicroRegEx.
## Test
Test pass by a test data set which contains 64 examples. Please run `python ./testing.py` for a detailed test.
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
## Acknowledge & Credits
1. Inspire by the [regex](https://github.com/xysun/regex) project of [xysun](https://github.com/xysun)
2. Some Documents from [regular\_expression\_engine](https://github.com/lihao98722/regular_expression_engine) project of [lihao98722](https://github.com/lihao98722/)
3. Test suite is based on [Glenn Fowler](http://www.research.att.com/~gsf/testregex/)'s regex test suites.
4. Test script is cloned from [regex](https://github.com/xysun/regex) project with some modification.
## Reference
* [Implementing Regular Expressions](https://swtch.com/~rsc/regexp/)