Regex 匹配单词和单词的任意数量的第一个连续字符
有没有更简单的方法来编写下面的正则表达式,特别是避免所有带有“?”可选字符的分组Regex 匹配单词和单词的任意数量的第一个连续字符,regex,Regex,有没有更简单的方法来编写下面的正则表达式,特别是避免所有带有“?”可选字符的分组 /^w(o(r(d)?)?)?$/ 它应符合以下要求: function rx_from_word($word='',$escapeNeeded=true) { $rx = ''; $i = strlen($word); while (--$i > -1) { if ($escapeNeeded && strpos('|/{}[]().*\\+^$',$wor
/^w(o(r(d)?)?)?$/
它应符合以下要求:
function rx_from_word($word='',$escapeNeeded=true) {
$rx = ''; $i = strlen($word);
while (--$i > -1) {
if ($escapeNeeded && strpos('|/{}[]().*\\+^$',$word{$i}) !== false) $char = '\\'.$word{$i};
// I'm not sure if I missed any special character above.
else $char = $word{$i};
if ($i > 0) $rx = '(' . $char . $rx . ')?';
else $rx = $char . $rx;
}
return $rx;
}
function rx_from_words($words=array(),$matchFull=false) {
$rx = $matchFull ? '^' : '';
foreach ($words as $word) $rx .= rx_from_word($word) . '|';
return substr($rx,0,-1) . ($matchFull ? '$' : '');
}
$words = array('horizontal','vertical','$10');
$rx = rx_from_words($words,1);
echo "<pre>$rx</pre>";
- w
- 沃
- 工作
- 话
- 西铁
- wd
- 求爱
- wrr
- 沃德
- 沃多
- 作战需求
- 研发部
- odr
/^h(o(r(i(z(o(n(t(a(l)?)?)?)?)?)?)?)?)?|v(e(r(t(i(c(a(l)?)?)?)?)?)?)?)$/
我正在使用ruby,但我认为这个问题适用于任何使用正则表达式的语言,所以我将感谢任何语言的答案。但是,我对perl知之甚少
我只发现了一个与我的问题类似的问题,但没有给出更好的解决方案。如果你用不同的方法来解决呢?例如(我不熟悉ruby,所以我将使用python):
或者类似于这些的东西如果你用不同的方式做会怎么样?例如(我不熟悉ruby,所以我将使用python):
或者类似的东西你可以用一个
或表达式来简化它:
/^(w|wo|wor|word)$/
或者通过从输入文本(伪代码)生成正则表达式来反转测试:
您可以使用或表达式简化它:
/^(w|wo|wor|word)$/
或者通过从输入文本(伪代码)生成正则表达式来反转测试:
虽然丑陋且难以阅读,但我会创建一个函数来为每个单词创建正则表达式。例如,如果是PHP,我会将其公式化如下:
function rx_from_word($word='',$escapeNeeded=true) {
$rx = ''; $i = strlen($word);
while (--$i > -1) {
if ($escapeNeeded && strpos('|/{}[]().*\\+^$',$word{$i}) !== false) $char = '\\'.$word{$i};
// I'm not sure if I missed any special character above.
else $char = $word{$i};
if ($i > 0) $rx = '(' . $char . $rx . ')?';
else $rx = $char . $rx;
}
return $rx;
}
function rx_from_words($words=array(),$matchFull=false) {
$rx = $matchFull ? '^' : '';
foreach ($words as $word) $rx .= rx_from_word($word) . '|';
return substr($rx,0,-1) . ($matchFull ? '$' : '');
}
$words = array('horizontal','vertical','$10');
$rx = rx_from_words($words,1);
echo "<pre>$rx</pre>";
函数rx\u from\u word($word='',$escapeNeeded=true){
$rx='';$i=strlen($word);
而(-$i>-1){
如果($escapeNeeded&&strpos('.\/{}[]().\\\+^$',$word{$i})!==false)$char='\\\'.$word{$i};
//我不确定我是否遗漏了上面的任何特殊字符。
else$char=$word{$i};
如果($i>0)$rx='('.$char.$rx')?';
else$rx=$char.$rx;
}
返回$rx;
}
函数rx_from_words($words=array(),$matchFull=false){
$rx=$matchFull?“^”:”;
foreach($words作为$word)$rx.=rx_from_word($word)。“|”;
返回substr($rx,0,-1)。($matchFull?“$”:”);
}
$words=array('horizontal'、'vertical'、'$10');
$rx=rx来自单词($words,1);
回音“$rx”;
哪个会输出
^h(o(r(i)(z)(o)(n(t(a(l)?)?)?)?)?)?)?)v(e(r(t(i)(c(a(l)?)?)?)?)?)?)??)\$(1(0)?)$
虽然丑陋且难以阅读,但我会创建一个函数来为每个单词创建正则表达式。例如,如果是PHP,我会将其公式化如下:
function rx_from_word($word='',$escapeNeeded=true) {
$rx = ''; $i = strlen($word);
while (--$i > -1) {
if ($escapeNeeded && strpos('|/{}[]().*\\+^$',$word{$i}) !== false) $char = '\\'.$word{$i};
// I'm not sure if I missed any special character above.
else $char = $word{$i};
if ($i > 0) $rx = '(' . $char . $rx . ')?';
else $rx = $char . $rx;
}
return $rx;
}
function rx_from_words($words=array(),$matchFull=false) {
$rx = $matchFull ? '^' : '';
foreach ($words as $word) $rx .= rx_from_word($word) . '|';
return substr($rx,0,-1) . ($matchFull ? '$' : '');
}
$words = array('horizontal','vertical','$10');
$rx = rx_from_words($words,1);
echo "<pre>$rx</pre>";
函数rx\u from\u word($word='',$escapeNeeded=true){
$rx='';$i=strlen($word);
而(-$i>-1){
如果($escapeNeeded&&strpos('.\/{}[]().\\\+^$',$word{$i})!==false)$char='\\\'.$word{$i};
//我不确定我是否遗漏了上面的任何特殊字符。
else$char=$word{$i};
如果($i>0)$rx='('.$char.$rx')?';
else$rx=$char.$rx;
}
返回$rx;
}
函数rx_from_words($words=array(),$matchFull=false){
$rx=$matchFull?“^”:”;
foreach($words作为$word)$rx.=rx_from_word($word)。“|”;
返回substr($rx,0,-1)。($matchFull?“$”:”);
}
$words=array('horizontal'、'vertical'、'$10');
$rx=rx来自单词($words,1);
回音“$rx”;
哪个会输出
^h(o(r(i)(z)(o)(n(t(a(l)?)?)?)?)?)?)?)v(e(r(t(i)(c(a(l)?)?)?)?)?)?)??)\$(1(0)?)$
什么编程语言?只进行字符串比较可能比正则表达式更容易。伪代码:substring(word,0,length(partial_-word))==partial_-word
这个经典问题最好通过反转字符串和模式来解决。使用Perl语法将变量插入模式,即“HORIZONTAL”=~/^$word/i
,它将匹配“h”、“ho”、“hor”等,但不匹配“horriplate”等。什么编程语言?只进行字符串比较可能比正则表达式更容易。伪代码:substring(word,0,length(partial_-word))==partial_-word
这个经典问题最好通过反转字符串和模式来解决。使用Perl语法将变量插入到模式中,即“HORIZONTAL”=“HORIZONTAL”=~/^$word/i
,它将匹配“h”、“ho”、“hor”等,但不匹配“horriplate”等。用于反转测试的+1。这很聪明。不幸的是,它很难在一个大文件中查找单词,因此它远不如普通正则表达式+1V用于反向测试。实际上,我希望从正则表达式本身获得一些递归语法,但在处理此类情况时,反向概念确实值得拥有。感谢+1逆转测试。这很聪明。不幸的是,它很难在一个大文件中查找单词,因此它远不如普通正则表达式+1V用于反向测试。实际上,我希望从正则表达式本身获得一些递归语法,但在处理此类情况时,反向概念确实值得拥有。谢谢