iogf / crocs

Python to Regex. Regex to Python. The yRegex for humans.
Apache License 2.0
519 stars 21 forks source link

Plain regex string (New class don't escape ) #6

Open eromoe opened 6 years ago

eromoe commented 6 years ago

I think this project is good for group many pattern together, for beginers .

  1. Usually we already have some regex pattern, don't want to rewrite them
  2. Usually we need some build-in pattern, such as \d , \s , \S , ? . It is no need to write all easy pattern in crocs way, just plain regex string is easy understand.

Now, crocs would escape all https://github.com/iogf/crocs/blob/a7098ae9bf9e0955373008ae8b92de29ea0dc578/crocs.py#L29

For example:

x = X()

no_lf = P(E('\n'))
to_lf = T(no_lf, 1)
xplus = T(no_lf, 1)
xstar = T(no_lf, 0)

p1_ng = NG('project', to_lf)
p2_ng = NG('project', xplus)

project = Any(
    P('(名称|编号)\s{0,5}(:|为)', p1_ng),    #  1
    P('test', p2,'test'),                               #  2
    P('test《?', p2,'》?test'),                    #  3
)

re.findall(project.to_regex,  xxx)

Here, 1 and 3 would not work.

iogf commented 6 years ago

It seems interesting. your idea is to allow raw regex to be inserted then have it displaying hints of what it would match?

eromoe commented 6 years ago

My purpose is to organize complex regex code more human readable. I think crocs is easy understand for regex beginner, reuse some regex to construct a new regex, and intuitive to organize regex code.

About display hints of raw regex , I think it may be difficult , however there is a project https://github.com/asciimoo/exrex

iogf commented 6 years ago

I see. Yes that is a great idea. Instead of having regex code snippets just have a library of functions that map to regex. is it? Then one could even share it on pypi.