在Python中使用Javascript正则表达式的瘦兼容层

js-regex的Python项目详细描述


js正则表达式

在Python中使用Javascript正则表达式的兼容层。

你知道正则表达式在不同的编程语言中会有所不同吗? 例如,让我们考虑模式"^abc$",它与字符串匹配 "abc"。但是字符串"abc\n"呢?它在Python中也匹配, 但不是Javascript!在

这和其他一些细微的差异对于跨语言来说是非常重要的 像jsonschema这样的标准,这就是js-regex存在的原因。在

工作原理

importreimportjs_regexre.compile("^abc$").search("abc\n")# matches, unlike JSjs_regex.compile("^abc$").search("abc\n")# does not match

在内部,js_regex.compile()替换了JS regex语法,它具有不同的 在Python中,无论使用Pythonregex语法都具有预期的含义。在

这只适用于.search()方法-没有等价的 .match()或{},用于Javascript正则表达式。在

我们还检查在Python中有效但JS无效的构造,例如 命名捕获组-并引发显式错误。有效的构造 在JS而不是Python中也可能会引发错误,因为我们仍然使用Python的 re.compile()引擎盖下的功能!在

下表改编自this larger version, 在JS和Python具有相同行为的其他语言和任何行中,都要进行注释。在

FeatureJavascriptPythonHandling
^{} (bell)noyesConverted to JS behaviour
^{}-^{} and ^{}-^{} (control characters)yesnoConverted to JS behaviour
^{} for digits, ^{} for word chars, ^{} for whitespaceasciiunicodeConverted to JS behaviour (including ^{}, ^{}, ^{} for negated classes)
^{} (end of line/string)at endallows trailing ^{}Converted to JS behaviour
^{} (start of string)noyesExplicit error, use ^{} instead
^{} (end of string)noyesExplicit error, use ^{} instead
^{} (positive lookbehind)new in ES2018yesAllowed
^{} (negative lookbehind)new in ES2018yesAllowed
^{}noyesExplicit error
^{}noyesExplicit error
^{}noyesExplicit error
^{} (Python named capture group)noyesNot detected (yet)
^{} (Python named backreference)noyesNot detected (yet)
^{} (JS named capture group)new in ES2018noError from Python, not translated (yet)
^{} (JS named backreference)new in ES2018noError from Python, not translated (yet)
^{} (case insensitive)^{} onlyyesExplicit error, compile with ^{} instead
^{} (^{} and ^{} match at line breaks)^{} onlyyesExplicit error, compile with ^{} instead
^{} (dot matches newlines)noyesExplicit error, compile with ^{} instead
^{} (free-spacing mode)noyesExplicit error, there is no corresponding mode in Javascript
Backreferences non-existent groups are an errornoyesFollows Python behaviour
Backreferences to failed groups also failnoyesFollows Python behaviour
Nested references ^{} through ^{}yesnoFollows Python behaviour

请注意,在许多情况下,只有Python的regex特性才会被视为 JS regex引擎的一个普通模式。目前我们提出了一个明确的错误 在这样的输入上,但可能会在将来的版本中转换为JS行为。在

变更日志

1.0.1-2019年10月17日

  • 允许在Python2上使用本机字符串。这实际上是无效的 但它只会存在几个月所以不管怎样。在

1.0.0-2019年10月4日
  • 现在我们认为这个特性是完整和稳定的,正如所有构造所建议的那样 for jsonschema模式受支持,所有Python端不兼容 被检测到。在
  • 编译后的模式现在缓存在python3上,与re.compile完全一样

0.4.0-2019年10月3日

0.3.0-2019年9月30日
  • 修正了对非尾随$的处理,例如在"^abc$|^def$"中两者都被转换
  • re.LOCALEre.VERBOSE标志添加了显式错误,它们没有等效的JS
  • 添加了使用Python-only regex特性的显式检查和错误

0.2.0-2019年9月28日

尽可能将JS语法转换为Python等效语法。在

0.1.0-2019年9月28日

初始版本,带有项目设置和非常基本的实现。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java将Scanner对象作为构造函数参数传递给UserInterface类   spring未能启动bean“subtocolWebSocketHandler”;嵌套的例外是java。lang.IllegalArgumentException:没有处理程序   Java EE应用程序中后台服务的多线程Java线程(设置守护进程和优先级)?   java Pull to refresh返回列表的第一个位置   无法将comparator类转换为java。可比的   java将json从servlet传递到dojo   JavaHibernate:将子类实例转换为超类实例   java警告调用servlet类时非法反射访问   java静态变量值   java@Entity和@embeddeble之间有什么区别   java将作业配置导入公共作业配置类(注释配置)   sql公共表表达式(以values语句开头)在java中给出错误   java在ImageJ中使用ImageProcessor   java PostgreSQL executeBatch()会随着时间的推移而变慢   java在安卓中以表格形式排列sqllite表数据?   java中SVG的swing定制呈现   java删除与另一个实体映射的实体   java何时/如何添加ListView适配器,使用back按钮恢复它?(片段)   java为什么IBinder和Binder之间的类型转换不是非法的?   java在方法参数列表中使用ArrayList或List