Table des matières
回复讨论(解决方案)

正则达人请进

Jun 23, 2016 pm 02:20 PM

说明:
分隔符 (),/,//

(a) b :  b,a b 均正确.括号里的词可有可无
(a/b) c :  c,a c,b c 均正确./前后的词是或的关系
a//b : a,b均正确

要求:
把以上符号表示的词语组合转换成正则表示法
例:((it is/’s) the/a) cat // (It is/’s) (the) animal


回复讨论(解决方案)

说明:
分隔符 (),/,//

(a) b :  b,a b 均正确.括号里的词可有可无
(a/b) c :  c,a c,b c 均正确./前后的词是或的关系
a//b : a,b均正确

要求:
把以上符号表示的词语组合转换成正则表示法
例:((it is/’s) the/a) cat // (It is/’s) (the) animal

不太理解你要表达的意思

首先(a)b匹配的是字符“ab”,子字符串“a”
其次(a/b)c匹配的是字符“a/bc”,子字符串“a/b”
再次a//b匹配的是字符串“a//b”



((it is/’s) the/a) cat // (It is/’s) (the) animal

你是想写正则表达式匹配
it is a cat
it is the cat
it's a cat
it's the cat
it is animal
it is the animal
it's animal
it's the animal
吧?

(it('s|\s+is)\s+(a|the)\s+cat)|(it('s|\s+is)\s+(animal|the\s+animal))

不太理解你要表达的意思

首先(a)b匹配的是字符“ab”,子字符串“a”
其次(a/b)c匹配的是字符“a/bc”,子字符串“a/b”
再次a//b匹配的是字符串“a//b”



((it is/’s) the/a) cat // (It is/’s) (the) animal

你是想写正则表达式匹配
it is a cat
it is the cat
it's a cat
it's the cat
it is animal
it is the animal
it's animal
it's the animal
吧?
除了上面这些,
cat
a cat
the cat
animal
the animal
都是对的。
我要一种转化方式,分隔符 (),/,// 这些是约定规则,并非正则语意.感谢1楼回复


做个事例便于理解

function pregRule($str){    $str = trim($str);    return '/^'.str_replace(array(') ','[',']','//','/',' '),array(')?\s*','(','){1}','|','|','\s+'),$str).'$/'; }$str = "(a) b";echo pregRule($str).'<br>';$str = "[a//b]";        #加个中括号,方便转换echo pregRule($str).'<br>';$str = "(a/b) c";echo pregRule($str).'<br>';$str = "((it [is/’s]) the/a) cat // (It [is/’s]) (the) animal";echo pregRule($str).'<br>';
Copier après la connexion
Copier après la connexion
Copier après la connexion


#result:
/^(a)?\s*b$/
/^(a|b){1}$/
/^(a|b)?\s*c$/
/^((it\s+(is|’s){1})?\s*the|a)?\s*cat\s+|\s+(It\s+(is|’s){1})?\s*(the)?\s*animal$/

前三种还凑合,第四个匹配就不忍心看了,请大家帮忙丰富这个函数,使之适应,可以适当修改语句

做个事例便于理解

function pregRule($str){    $str = trim($str);    return '/^'.str_replace(array(') ','[',']','//','/',' '),array(')?\s*','(','){1}','|','|','\s+'),$str).'$/'; }$str = "(a) b";echo pregRule($str).'<br>';$str = "[a//b]";        #加个中括号,方便转换echo pregRule($str).'<br>';$str = "(a/b) c";echo pregRule($str).'<br>';$str = "((it [is/’s]) the/a) cat // (It [is/’s]) (the) animal";echo pregRule($str).'<br>';
Copier après la connexion
Copier après la connexion
Copier après la connexion


#result:
/^(a)?\s*b$/
/^(a|b){1}$/
/^(a|b)?\s*c$/
/^((it\s+(is|’s){1})?\s*the|a)?\s*cat\s+|\s+(It\s+(is|’s){1})?\s*(the)?\s*animal$/

前三种还凑合,第四个匹配就不忍心看了,请大家帮忙丰富这个函数,使之适应,可以适当修改语句

大概明白了

何必呢,你确定你设置的这些所谓的规则使用者很好懂么?

你是想做一个类似自动阅卷的功能吧,我猜设置答案的老师会被搞晕菜的

做个事例便于理解

function pregRule($str){    $str = trim($str);    return '/^'.str_replace(array(') ','[',']','//','/',' '),array(')?\s*','(','){1}','|','|','\s+'),$str).'$/'; }$str = "(a) b";echo pregRule($str).'<br>';$str = "[a//b]";        #加个中括号,方便转换echo pregRule($str).'<br>';$str = "(a/b) c";echo pregRule($str).'<br>';$str = "((it [is/’s]) the/a) cat // (It [is/’s]) (the) animal";echo pregRule($str).'<br>';
Copier après la connexion
Copier après la connexion
Copier après la connexion


#result:
/^(a)?\s*b$/
/^(a|b){1}$/
/^(a|b)?\s*c$/
/^((it\s+(is|’s){1})?\s*the|a)?\s*cat\s+|\s+(It\s+(is|’s){1})?\s*(the)?\s*animal$/

前三种还凑合,第四个匹配就不忍心看了,请大家帮忙丰富这个函数,使之适应,可以适当修改语句

第四个主要的问题在于把“/”换成“|”

a/b
ca/bd
第一种情况下不需要考虑太多  直接把“/”换成“|”就ok
第二种情况下,就有要考虑的了。你是想得到cad,cbd还是想得到ca,bd呢?所以这个时候就不能只考虑“/”换成“|”

下面是你的第四个替换之后的表达式
/^( (it\s+(is|’s){1})?\s*the|a)?\s*cat\s+|\s+(It\s+(is|’s){1})?\s*(the)?\s*animal$/
只看前半部分
( (it\s+(is|’s){1})?\s*the|a)?\s*cat\s+ 中的 
(it\s+(is|’s){1})?\s*the|a)
你想得到it\s+(is|’s){1})?\s*匹配的字符然后拼接the或者a
但实际上表达式呈现出来的是你想得到it\s+(is|’s){1})?\s*the或者a

你想要  i want(the|a),但实际上你却说  i want the|a

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Article chaud

Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Article chaud

Repo: Comment relancer ses coéquipiers
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
1 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌

Tags d'article chaud

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

11 meilleurs scripts de raccourcissement d'URL PHP (gratuit et premium) 11 meilleurs scripts de raccourcissement d'URL PHP (gratuit et premium) Mar 03, 2025 am 10:49 AM

11 meilleurs scripts de raccourcissement d'URL PHP (gratuit et premium)

Introduction à l'API Instagram Introduction à l'API Instagram Mar 02, 2025 am 09:32 AM

Introduction à l'API Instagram

Travailler avec les données de session Flash dans Laravel Travailler avec les données de session Flash dans Laravel Mar 12, 2025 pm 05:08 PM

Travailler avec les données de session Flash dans Laravel

Construisez une application React avec un Laravel Back End: Partie 2, React Construisez une application React avec un Laravel Back End: Partie 2, React Mar 04, 2025 am 09:33 AM

Construisez une application React avec un Laravel Back End: Partie 2, React

Misque de réponse HTTP simplifié dans les tests Laravel Misque de réponse HTTP simplifié dans les tests Laravel Mar 12, 2025 pm 05:09 PM

Misque de réponse HTTP simplifié dans les tests Laravel

Curl dans PHP: Comment utiliser l'extension PHP Curl dans les API REST Curl dans PHP: Comment utiliser l'extension PHP Curl dans les API REST Mar 14, 2025 am 11:42 AM

Curl dans PHP: Comment utiliser l'extension PHP Curl dans les API REST

12 meilleurs scripts de chat PHP sur Codecanyon 12 meilleurs scripts de chat PHP sur Codecanyon Mar 13, 2025 pm 12:08 PM

12 meilleurs scripts de chat PHP sur Codecanyon

Notifications à Laravel Notifications à Laravel Mar 04, 2025 am 09:22 AM

Notifications à Laravel

See all articles