如何扩展尝试/捕获以处理正则表达式错误

发布于 2025-01-30 06:09:25 字数 575 浏览 2 评论 0原文

我正在编写一个使用大量正则表达式的机器人,有时会根据html获取一条信息(它被随机制定以阻止像我这样的人找到我想要的东西并接受...),所以我会去通过许多正则表达式获得x或y的数据,例如名称,得分,大小,重量等

。一个捕获块,只是为了让我记录表达式,我正在查看的HTML以及其他信息而不会丢失错误。

只是我有很多正则表达式,我不想在将正则执行之前设置全局标志,以便如果捕获量失败,我会检查全局标志,如果是真的,我知道那是正等式执行,因此我可以记录与其他可能触发的信息不同的信息。

或者您可能会说这是最好的方法,而不是覆盖系统代码等。

我刚出院 - 再次,我确实相信我去年这次在那里,病房上没有一个动力淋浴,无论如何他们给我的吗啡非常强大,所以请不要怪我一个愚蠢的问题,因为在寻求帮助之前,我可能会更多地寻找答案。我认为旗帜方式是前进的最佳方法,而不是扩展我可能不应该的代码,但也许我已经错过了正则失败。 #

如果我走标志方式,我可以在检查该标志的捕获块中放置一个通用错误处理程序,如果发现它会在我的日志文件中登录正则是the Regex,HTML和XYZ其他错误,但是我希望在日志文件中进行详细的错误报告。

想法?

事先感谢您的任何帮助。

I am writing a BOT that uses a lot of Regular Expressions, sometimes to get one piece of information, depending on the HTML (it gets randomised to stop people like me from finding what I want and taking it ... ) so I will go through a number of regular expressions to obtain X or Y piece of data, such as a name, score, size, weight etc etc.

Is there a way of extending the TRY/CATCH handler so that if it fails on a Regular Expression I have a catch block just for that which would let me log the expression, the HTML I was looking at, and other info without throwing the error.

It's just I have a lot of regular expressions and I don't want to set global flags before the regex execution so that if it fails in the catch, I check the global flag and if true, I know it was a regex that was being executed, so I can then log different info than if something else might have triggered it.

Or you may say that is the best way rather than overriding system code etc.

I have just come out of hospital - again, I do believe I was in there this time last year and not a single power shower on the ward worked, anyway the morphine they have given me is pretty strong so please don't blame me for a stupid question as I would have probably searched a lot more for an answer before asking for help. I am thinking that the flag way is the best way forward rather than extending code I probably shouldn't be but maybe there is already a RegEx failure catch I've missed. #

If I go the flag way I can put a generic error handler in the catch block that checks for that flag and if it finds it logs the regex, html and x y z all in my log file rather than throwing the error which I would do for other errors but with this I want a detailed error report in my log file.

Ideas?

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文