Products
GG网络技术分享 2025-03-18 16:15 3
For those regex experts out there, can you help me tweak this regex to always add a whitespace between two matching characters.
My attempt is to resolve the interpolation security issue with Vue.js for data passed from the server side (Vue.js will try to evaluate anything between two curly braces). The goal is:
My str_replace solution (which accomplishes goal #1 only)
str_replace([\'{\', \'}\', \'{\', \'}\', \'{\', \'}\' ],
[\'{ \', \'} \', \'{ \', \'} \', \'{ \', \'} \'],
$value
);
My attempted regex thus far:
preg_replace(\'/({|}|{|}|{|})(\\S)/\', \'$1 $2\', $value);
So it checks for any matching character that isn\'t followed by whitespace and inserts whitespace between the two characters.
The regex works in most cases, but fails when you have three or more matching characters.
Ex: {{{
returns { {{
but the expected output would be { { {
.
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议对于那些正则表达式专家,你能帮我调整这个正则表达式,总是在两个匹配的字符之间添加一个空格。 </ p>
我的尝试是解决Vue.js对于从服务器端传递的数据的插值安全问题(Vue.js将尝试评估两个花括号之间的任何内容)。 目标是:</ p>
</ ol>
我的str_replace解决方案(仅实现目标#1)</ p>
str_replace([\'{\',\'}\',\' &amp;#123;\',\'&amp;#125;\',\'&amp;#x7b;\',\'&amp;#x7d;\'],
[\'{\',\'}\',\'&amp;#123;\' ,\'&amp;#125;\',\'&amp;#x7b;\',\'&amp;#x7d;\'],
$ value
);
</ code> </ pre>
我到目前为止尝试的正则表达式:</ p>
preg_replace(\'/({|} |&amp;#123; |&amp;#125; |&amp;#x7b; |&amp; #x7d;)(\\ S)/\',\'$ 1 $ 2\',$ value); </ code> </ pre>
因此它会检查任何未跟随的匹配字符 空格并在两个字符之间插入空格。</ p>
正则表达式在大多数情况下有效,但在有三个或更多匹配字符时失败。</ p>
Ex : {{{</ code>返回 {{{</ code>,但预期输出为 {{{</ code>。</ p>
</ div>
网友观点:
It doesn\'t work as you expect it because the first two {
characters match the regex
, the replacement is made then the search continues with the 3rd character of the input string
You can solve this by turning the second parenthesis into a forward assertion. This way, the second }
is not consumed on the first match and the next search starts with the second character of the input string:
preg_replace(\'/({|}|{|}|{|})(?=\\S)/\', \'$1 \', $value);
There is only one capture group this way, $2
is always empty and it is not needed any more in the replacement string.
See it in action: https://3v4l.org/CaMHS. The +++
markers were added to show that the third {
doesn\'t match the regex
and no replacement occurs for it.
数据科学入门必读:如何使用正则表达式?
选自Dataquest,作者:Alex Yang,机器之心编译
正则表达式对数据处理而言非常重要。近日,Dataquest 博客发布了一篇针对入门级数据科学家的正则表达式介绍文章,通过实际操作详细阐述了正则表达式的使用方法和一些技巧。
数据科学家的一部分使命是操作大量数据。有时候,这些数据中会包含大量文本语料。我们可以采用人工方式,亲自阅读,但我们也可以利用 Python 的力量。毕竟,代码存在的意义就是自动执行任务。
即便如此,从头开始写一个脚本也需要大量时间和精力。这就是正则表达式的用武之地。正则表达式(regular expression)也被称为 RE、regex 和 regular pattern,这是一种让我们能快速筛查和分析文本的紧凑型语言。正则表达式始于 1956 年——Stephen Cole Kleene 创造了它并将其用于描述人类神经系统的 McCulloch-Pitts 模型。到了 60 年代,Ken Thompson 将这种标记方法添加到了一个类似 Windows 记事本的文本编辑器中,自那以后,正则表达式不断发展壮大。
正则表达式的一大关键特征是其经济实用的脚本。你甚至可以将其看作是代码中的捷径。没有它,我们就要码更多代码才能实现相同的功能。学习本教程需要基本的 Python 知识。如果你理解 if-else 语句、while 和 for 循环、列表(list)和字典(dictionary),你就能读懂本教程的大部分内容。你也需要一个代码编辑器,比如 Visual Code Studio、PyCharm 或 Atom。此外,了解一点 pandas 的基本知识会很有帮助,这样在我们解读每一行代码时你才不会迷失方向。如果你需要学学 pandas,可以参考这个教程:https://www.dataquest.io/blog/pandas-python-tutorial/
学习完本教程后,你将熟悉一些正则表达式的工作原理,并能使用其基本模式和 Python 的 re 模块提供的函数来分析字符串。我们还将体验正则表达式和 pandas 库高效处理大规模无组织数据集的能力。
现在,我们来看看正则表达式的能力。
介绍我们的数据集
我们将使用来自 Kaggle 的 Fraudulent Email Corpus(欺诈电子邮件语料库)。其中包含 1998 年到 2007 年之间发送的数千封钓鱼邮件。这些邮件读起来很有意思。我们首先将使用单封邮件学习基本的正则表达式命令,然后我们会对整个语料库进行处理。
语料库地址:https://www.kaggle.com/rtatman/fraudulent-email-corpus
介绍 Python 的正则表达式模块
首先,准备数据集:打开那个文本文件,将其设置成「只读」,然后读取它。我们也为其分配了一个变量 fh,表示文件句柄(file handle)。
Demand feedback