[英]PHP Regex help required
I have a request for a bit of help, not so complicate as I think but I could not figure out:我请求一些帮助,没有我想象的那么复杂,但我想不通:
This is my regex pattern:这是我的正则表达式模式:
/:\\s*'([^:]*)'/g
(I use it without flag g by preg_match_all()) /:\\s*'([^:]*)'/g
(我用 preg_match_all() 不带标志 g 使用它)
This is the string to search (usually from jqValidate):这是要搜索的字符串(通常来自 jqValidate):
{ messages : { required : 'This 'asdf' "asdf" field is required!', dateISO : 'This is a test...' } , rules : { dateISO : true , required : true } }
This is what I get and want to get:这就是我得到并想要得到的:
array(
0 => array(
0 => : 'This 'asdf' "asdf" field is required!'
1 => : 'This is a test...'
)
1 => array(
0 => This 'asdf' "asdf" field is required!
1 => This is a test...
)
)
This is the problem:这就是问题:
This pattern - I spent hours to figure out as is (I´m not well practiced) - works good but only as long as I do not need a : (colon).这种模式 - 我花了几个小时来弄清楚(我没有很好地练习) - 效果很好,但前提是我不需要 :(冒号)。 If I use a colon in a message text between single quotes, that message does not match any more.
如果我在单引号之间的消息文本中使用冒号,则该消息不再匹配。
Mostly I tried to play around with the negation of the colon but I had no idea how to negate a group like "match all occurrences but colons, "only if" they are not lead by at least one single quote and anything between the single quote and the colon".大多数情况下,我尝试使用冒号的否定,但我不知道如何否定一个组,例如“匹配除冒号之外的所有出现,“仅当”它们不是由至少一个单引号和单引号之间的任何内容引导和冒号”。
To make it a bit more clear what I meant above:为了更清楚地说明我上面的意思:
Example: This is a plausible use of a colon in a jqValidate message.
'Example': We probably do not use a colon together with single quotes like this.
Any 'text' here: This is a very unusual 'portion of text'!
I hope you see, what is my problem.我希望你明白,我的问题是什么。 Any useful help would be very appreciated.
任何有用的帮助将不胜感激。
Thanx in advance,提前谢谢,
Regards Ingmar问候英格玛
This Should Work:这应该有效:
Regexp:正则表达式:
/(?<=required)\s:\s+\'(.+)\',|(?<=dateISO)\s:\s+\'(.+)\'/g
Input:输入:
{ messages : { required : 'This 'asdf' "asdf" field is required!', dateISO : 'This is a test...' } , rules : { dateISO : true , required : true } }
Output:输出:
This 'asdf' "asdf" field is required!
This is a test...
PHP Code: PHP代码:
<?php
$re = '/(?<=required)\s:\s+\'(.+)\',|(?<=dateISO)\s:\s+\'(.+)\'/';
$str = '{ messages : { required : \'This \'asdf\' "asdf" field is required!\', dateISO : \'This is a test...\' } , rules : { dateISO : true , required : true } }';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);
?>
See: https://regex101.com/r/UD7pgq/2参见: https : //regex101.com/r/UD7pgq/2
For those of you, who are interested in the solution I worked out my self and for myself in future time, here is how I solved it:对于那些对我自己和将来自己制定的解决方案感兴趣的人,以下是我的解决方法:
Given string is (* Before you comment the not escaped quotes in quotes, read the end of my posting!):给定的字符串是 (* 在用引号评论未转义的引号之前,请阅读我的帖子的结尾!):
{ messages : { required : 'This 'asdf' "asdf" field is required!', dateISO : 'This is a test...' } , rules : { dateISO : true , required : true } }
Forget about this!!!忘记这个!!! I fooled my self, point No. 1 is not needed at all, as (in case of jqValidate) there is no other section existing like "messages", letting the DEV define TEXT-IN-QUOTES!!!
我欺骗了我自己,根本不需要第 1 点,因为(在 jqValidate 的情况下)没有其他部分像“消息”一样存在,让 DEV 定义 TEXT-IN-QUOTES!!! Just jump over to point No. 2!!!
直接跳到第 2 点!!!
But of course the basic principle remains.但当然,基本原则仍然存在。
/* /*
1. Get only the messages out of the whole trimmed string:
1. 仅从整个修剪后的字符串中获取消息:
^(?:\\{\\s*messages\\s*:\\s*\\{){1}(.*)(?:\\}\\s*,(?:.*)\\}){1}$
The resulting array of preg_match will contain the string to work with on index
1 :
preg_match 的结果数组将包含要在索引
1上使用的字符串:
required : 'This 'asdf' "asdf" field is required!', dateISO : 'This is a test...'
*/ */
:\\s*\\'((?:(?!\\'\\s*,(?:.*):\\s*\\'(?:.*)).)+)\\'
The resulting array of preg_match_all will contain now ALL messages in sub-array with index 1 and you can do whatever you want (clean, replace " with ', trim(), addslashes(), rebuild it, replace faulty entries in DB, etc...).生成的 preg_match_all 数组现在将包含索引为1 的子数组中的所有消息,你可以做任何你想做的事情(清理、用 '、trim()、addslashes() 替换 "、重建它、替换数据库中的错误条目等……)。
(*) BTW: In this system the admin-users (not the developers) already wrote thousands of jqValidate rules and messages to the DB and yet there was no validation for faulty entries like this in the system. (*) 顺便说一句:在这个系统中,管理员用户(不是开发人员)已经向数据库写入了数千条 jqValidate 规则和消息,但系统中没有对此类错误条目进行验证。 Before I search and clean all of those entries by hand or let the admin-users rewrite their work of years, I choose the way to clean it by a tiny little script with Regular Expressions.
在我手动搜索和清理所有这些条目或让管理员用户重写他们多年的工作之前,我选择了通过一个带有正则表达式的小脚本来清理它的方式。 That´s what Regular Expressions are for... At least, the way I understood.
这就是正则表达式的用途......至少,我理解的方式。
If you are interested in how it looks: regex101.com如果您对它的外观感兴趣: regex101.com
And: Yes , you can use single quotes in jqValidate - as long as you escape it (eg by addslashes()):并且:是的,您可以在 jqValidate 中使用单引号 - 只要您对其进行转义(例如通过addslashes()):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.