【发布时间】:2018-08-06 22:02:29
【问题描述】:
我已经尝试了几个小时来完成此任务,但我仍处于学习 RegEx 的初期,因此请向这里的专家寻求帮助。 如何仅匹配开头有美元符号的字符串? 我已经用这个正则表达式取得了部分成功:
(?<=\$)\[(?=[^\]]*[a-z])([a-zA-Z_]+[\w]+)\]
在这个代码块上:
<?php
$options[$list[_capture_me]][]=array($list[_captureme],$list[_capture_me]);
$option_names[$list[_capture_me]]=$list[_captureme];
$product_name=$list[capture_me];
$product_name=$list[0];
$product_name=$list[DONTCAPTUREME];
$product_name=$list[CapTureMe];
$product_name=$list[CapTurEME];
$actionKey = $get[DONT_CAPTURE_ME];
if(array_key_exists(DONT_CAPTURE_ME,$get)) {
$actionKey = $get[DONT_CAPTURE_ME];
if(array_key_exists($actionKey,$this->arrPageActions)){
$this->arrAllTemplatesAndAttributes[$templateID][captureme]['Capture_Me'][$res[option_values_id]] = $res[DONT_CAPTURE_ME];
$old_faq_id.="$result[faq_id]&";
$result['toc']="<b><a href=$PHP_SELF#$result[faq_id]>$result['question']</a></b>";}
$result['toc']="<b><a href=$PHP_SELF#$result[faq_id]>$result[QUESTION]</a></b>";}
$login_groups_id = $check_admin[login_groups_id];
?>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
function check_input(field_name, field_size, message) {
if ($elements[capture_me] && (form.elements[dont_capture].type != "hidden") && $form.elements[capture_me]) {
var field_value = form.elements[dontcapture].value;
var field_value = $form.elements[captureme].value;
if (field_value == '' || field_value.length < field_size) {
error_message = error_message + "* " + message + "\n";
error = true;
}
}
}
正如您在DEMO 中看到的那样,它也匹配括号内的字符串,即使它们的开头没有美元符号,我需要防止这种情况发生。 我尝试像这样添加积极的lookbehind:
(?<=\$\w+)
但这在这个引擎中不起作用,错误:
lookbehind 中的量词使其宽度不固定
有什么办法可以做到吗?
顺便说一句,我这样做是因为我的最终目标是使用替换为括号中的所有字符串添加单引号,因此像这样的行 $product_name=$list[CapTureMe]; 将变为 $product_name=$list['CapTureMe'];,例如,使用以下代码:
['\1']
谢谢
【问题讨论】:
-
regex101.com/r/5OeJSw/1 喜欢这个?
-
哇。看起来不错,但它仍然不匹配
$form.elements[capture_me]例如 -
这甚至是有效的 PHP 语法吗?我以为
.是字符串连接。 -
你是对的,它根本不是有效的 php 或 js,只是我正在测试的东西。请发表你的答案,让我以某种方式感谢你:)
-
Regex 不是解析复杂语言的工具。 PHP 有一个tokenizer,使用它。