[英]Regex using from inside of quotation
This is my current regex attempt: 这是我当前的正则表达式尝试:
$input_lines = 'Data1("begin Data2(hey); end", "hi");';
preg_match_all("/(.*?)[(](.*?)[)][;]/", $input_lines, $output_array);
I want this regex to get: 我希望此正则表达式得到:
"begin Data2(hey); end", "hi"
On the third array, but it just gets: 在第三个数组上,但是得到:
"begin Data2(hey
I want to not get anything inside of these double quotes (not close when we have );
我不想在这些双引号内得到任何东西(当有时不要关闭
);
inside of " "
). 在
" "
内部)。
Just use a simple regex to match everything( .
match anything except new line; *
between 0 and more times; Also note it's greedy) between two quotes, eg 只需使用一个简单的正则表达式即可匹配所有内容(
.
匹配除换行符以外的所有内容; *
0次至更多次之间; *
注意贪婪)两个引号之间,例如
$input_lines = 'Data1("begin Data2(hey); end", "hi");';
preg_match('/(".*")/', $input_lines, $matches);
print_r($matches);
You could extend your regular expression so that it first checks if there is a double quote immediately after the opening bracket, and if so to require that there is one just before the closing bracket: 您可以扩展正则表达式,以便它首先检查在右括号后面是否有双引号,如果是这样,则要求在右括号之前有一个双引号:
$input_lines = "Data1(\"begin Data2(hey); end\", \"hi\");";
preg_match_all('/(.*?)[(](".*?"|.*?)[)];/', $input_lines, $output_array);
print_r ($output_array);
Output: 输出:
Array
(
[0] => Array (
[0] => Data1("begin Data2(hey); end", "hi");
)
[1] => Array (
[0] => Data1
)
[2] => Array (
[0] => "begin Data2(hey); end", "hi"
)
)
Probably you'll want to also have this working with single quotes, so you would add a possibility for that: 可能还需要使用单引号对此进行处理,因此您可以添加以下可能性:
$input_lines = "Data1('begin Data2(hey); end', 'hi');";
preg_match_all('/(.*?)[(](".*?"|\'.*?\'|.*?)[)];/', $input_lines, $output_array);
print_r ($output_array);
Then, you would also want to deal correctly with escaped quotes inside the input data, so you would extend it to this: 然后,您还希望正确处理输入数据中的转义引号,因此您可以将其扩展为:
$input_lines = "Data1('begin Data2(\'hey\'); end', 'hi');";
preg_match_all('/(.*?)[(](".*?(?:[\\\\]".*?)*"|\'.*?(?:[\\\\]\'.*?)*\'|.*?)[)];/',
$input_lines, $output_array);
print_r ($output_array);
Output would be: 输出为:
Array
(
[0] => Array (
[0] => Data1('begin Data2(\'hey\'); end', 'hi');
)
[1] => Array (
[0] => Data1
)
[2] => Array (
[0] => 'begin Data2(\'hey\'); end', 'hi'
)
)
Note that this way you will also still match strings that do not contain such quotes, eg 请注意,通过这种方式,您仍将匹配不包含此类引号的字符串,例如
$input_lines = "Data1(mydata);";
Would still match and give expected results. 仍会匹配并给出预期结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.