正则达人请进
说明:
分隔符 (),/,//
(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>';
#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>';
#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>';
#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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
