如何使用正则表达式匹配单引号或双引号中的字符串 [英] How to match string in single or double quoted using regex

查看:177
本文介绍了如何使用正则表达式匹配单引号或双引号中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个匹配字符串的正则表达式,如下所示:

I'm trying to write a regex that matches strings as the following:

translate("这里有一些文字")

translate('这里有一些文字')

我已经做到了:

preg_match ('/translate\("(.*?)"\)*/', $line, $m) 

但是如果有单引号,而不是双引号如何添加.它应该匹配为单引号,双引号.

but how to add if there are single quotes, not double. It should match as single, as double quotes.

推荐答案

你可以去:

translate\( # translate( literally
(['"])      # capture a single/double quote to group 1
.+?         # match anything except a newline lazily
\1          # up to the formerly captured quote
\)          # and a closing parenthesis

在 regex101.com 上查看 这种方法的演示.
<小时>在 PHP 中,这将是:

See a demo for this approach on regex101.com.


In PHP this would be:

<?php

$regex = '~
            translate\( # translate( literally
            ([\'"])     # capture a single/double quote to group 1
            .+?         # match anything except a newline lazily
            \1          # up to the formerly captured quote
            \)          # and a closing parenthesis
         ~x';

if (preg_match($regex, $string)) {
    // do sth. here
}
?>

注意您不需要对方括号 ([]) 中的两个引号进行转义,我只为 Stackoverflow 美化器做了它.
但请记住,这很容易出错(空格、转义引号呢?).<小时>在评论中出现了讨论,你不能说除了第一个被捕获的组.嗯,是的,你可以(感谢奥巴马在这里),该技术被称为 脾气暴躁令牌 可以通过环视实现.考虑以下代码:

Note that you do not need to escape both of the quotes in square brackets ([]), I have only done it for the Stackoverflow prettifier.
Bear in mind though, that this is rather error-prone (what about whitespaces, escaped quotes ?).


In the comments the discussion came up that you cannot say anything BUT the first captured group. Well, yes, you can (thanks to Obama here), the technique is called a tempered greedy token which can be achieved via lookarounds. Consider the following code:

translate\(
(['"])
(?:(?!\1).)*
\1
\)

它打开一个具有否定前瞻的非捕获组,确保不匹配以前捕获的组(本例中为引用).
这消除了像 translate("a"b"c"d") 这样的匹配(参见 这里有一个演示).<小时>匹配所有给定示例的最终表达式是:

It opens a non-capturing group with a negative lookahead that makes sure not to match the formerly captured group (a quote in this example).
This eradicates matches like translate("a"b"c"d") (see a demo here).


The final expression to match all given examples is:

translate\(
(['"])
(?:
   .*?(?=\1\))
)
\1
\)

这篇关于如何使用正则表达式匹配单引号或双引号中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆