简体   繁体   English

你如何编写和测试正则表达式?

[英]How do you write and test your regular expressions?

是他们的任何向导或工具来创建和测试PHP的正则表达式,因为它是如此困难:(?谢谢:)

RegexBuddy is a widely popular app for this purpose. RegexBuddy是一个广受欢迎的应用程序。 It also costs $40 and only runs on Windows. 它也需要40美元,只能在Windows上运行。

For powerful free alternatives, see this answer . 对于强大的免费替代品,请参阅此答案

reAnimator is a nice tool to visualize your regex as a state machine- I find it useful sometimes. reAnimator是一个很好的工具,可以将你的正则表达式可视化为状态机 - 我觉得它有时很有用。

Python also allows you to view a regex parse tree , which can be helpful if you learn to read it. Python还允许您查看正则表达式解析树 ,如果您学习阅读它可能会有所帮助。

Unit testing with example data. 使用示例数据进行单元测试 Create two arrays, one with matching data, and one with non-matching data if necessary to test edge cases. 创建两个数组,一个具有匹配的数据,另一个具有不匹配的数据,以便测试边缘情况。

Trial and 试用和 error 错误 success. 成功。

Because I've spent the time to actually learn it, instead of relying on something else to do it for me. 因为我花了很多时间来实际学习它,而不是依靠别的东西来为我做。

Same applies to any language/tool - take a bit of time to learn the syntax and general ethos, and you'll be far more productive than relying on intellisense, code hinting, and so on. 同样适用于任何语言/工具-需要一点时间来学习语法和一般的精神,你会更加富有成效不是依靠智能感知,代码提示,等等。

There are powerful online tools . 有强大的在线工具 Offline, 离线,

  • The Regex Coach is a great free offline regex tool that I use fairly regularly. 正则表达式教练是一个很好的免费离线正则表达式工具,我经常使用。
  • I like RegEx Buddy also, but it costs $40 and I'm cheap. 我也喜欢RegEx Buddy ,但它的价格是40美元而且我很便宜。

Expresso is free Windows program and gives nice breakup and explanation of the regex under analysis. Expresso是免费的Windows程序,可以很好地解析正在分析的正则表达式。

For online tools that you can run right away from a browser, see this answer . 对于可以立即从浏览器运行的在线工具,请参阅此答案

我一直用这个: http//gskinner.com/RegExr/

I've written my own tool: Regular Expression Tester . 我编写了自己的工具: Regular Expression Tester Unlike many other web-based tools, this one can break a regex down into tokens and describe what each token is doing. 与许多其他基于Web的工具不同,这个工具可以将正则表达式分解为令牌并描述每个令牌正在做什么。 It's great for examining new expressions, or expressions that you wrote a long time ago and don't quite remember. 它非常适合检查你很久以前写过的新表达式或表达式,并且不太记得。

Regex Buddy is overkill ($40) and works only on Windows. Regex Buddy过度使用(40美元),仅适用于Windows。 It was a good choice back in 2009 maybe. 这可能是2009年的一个不错的选择。

Now we have free powerful online tools to build and test regular expressions. 现在我们有免费的强大在线工具来构建和测试正则表达式。 Regex101 is one of them: Regex101就是其中之一:

  • lets you select the RE engine (PCRE, JavaScript, Python) 让你选择RE引擎(PCRE,JavaScript,Python)
  • colorizes the matches 将比赛着色
  • explains the regexp on the fly 在飞行中解释正则表达式
  • has a debugger 有一个调试器
  • can create permalinks to the regexp playground. 可以创建regexp游乐场的永久链接。

More regexp testing tools in my other answer . 我的其他答案中有更多正则表达式测试工具。

Trial and error. 试错。

And print_r . print_r

我真的很喜欢RegexPal ,它简单,清晰,无需安装,可在线免费使用。

Online... there's an ajax regex checker with js/pcre/posix implementations, that checks as you type.. way cool. 在线...有一个带js / pcre / posix实现的ajax正则表达式检查器,在你输入时进行检查..方式很酷。

http://www.rexv.org http://www.rexv.org

I used to use The Regex Coach. 我曾经使用The Regex Coach。 But because it's Perl based and most of the time I'm testing .NET regular expressions, I now use this online .NET regular expression tester . 但是因为它是基于Perl的,并且大多数时候我正在测试.NET正则表达式,所以我现在使用这个在线.NET正则表达式测试器

我喜欢emacs重建者。

Since you're talking about PHP, you may be interested in Codebench . 既然你在谈论PHP,你可能会对Codebench感兴趣。 It is a tool, not specifically to break down regexes (you've got a lot of those listed already), but to benchmark them. 它是一个工具,不是专门打破正则表达式(你已经有很多已经列出的那些),但要对它们进行基准测试。 Since it is rather generic, you can also compare non-regex solutions as often native string functions are faster. 由于它非常通用,您还可以比较非正则表达式解决方案,因为本机字符串函数通常更快。 Moreover, it allows you to benchmark against multiple subjects (targets) as well. 此外,它还允许您对多个主题(目标)进行基准测试。 Hope you find it useful. 希望你觉得它有用。

I'm using unit-testing. 我正在使用单元测试。 That way, I can grow my regex incrementally, being certain that the first cases I tested still pass. 这样,我可以逐步增加我的正则表达式,确保我测试的第一个案例仍然通过。 And if ever I have to modify it, I have all my tests to back me up. 如果我不得不修改它,我有我所有的测试来支持我。

对于在线测试Regx http://www.regexr.com/使用此站点,如果您的regx工作,那么您可以使用preg_match()函数在writecodeonline.com上检查它的php。

I wrote a python library to accomplish this, it is under cloudtb.re 我写了一个python库来完成这个,它在cloudtb.re下

text = 'so foo is the opposite of bar but without foo there is no bar?'
exp = '(foo).*?(bar)'
searched = cre.research(exp, text)
print(searched)

pprinted正则表达式

I generally use Rubular when I'm working on testing a regular expression. 当我正在测试正则表达式时,我通常使用Rubular You could also try txt2re.com , it can be handy for helping you figure out an expression and can even generate relevant PHP code. 您也可以尝试使用txt2re.com ,它可以帮助您找出表达式,甚至可以生成相关的PHP代码。

Here is another online regular expression tester for Java: 这是Java的另一个在线正则表达式测试器:

http://www.fileformat.info/tool/regex.htm http://www.fileformat.info/tool/regex.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM