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

AI Hentai Generator
Generate AI Hentai for free.

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

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
