【问题标题】:PHP Regex help required需要 PHP 正则表达式帮助
【发布时间】:2019-12-10 13:47:50
【问题描述】:

我需要一点帮助,没有我想的那么复杂,但我想不通:

这是我的正则表达式模式:

/:\s*'([^:]*)'/g(我在 preg_match_all() 没有标志 g 的情况下使用它)

这是要搜索的字符串(通常来自 jqValidate):

{ messages : { required : 'This 'asdf' "asdf" field is required!', dateISO : 'This is a test...' } , rules : { dateISO : true , required : true } }

这是我得到和想要得到的:

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...
    )
)

这就是问题所在:

这种模式 - 我花了几个小时才弄清楚(我没有很好地练习) - 效果很好,但前提是我不需要 :(冒号)。 如果我在单引号之间的消息文本中使用冒号,则该消息不再匹配。

大多数情况下,我试图玩弄冒号的否定,但我不知道如何否定“匹配除冒号以外的所有事件”这样的组,“仅当”它们不是由至少一个单引号和介于两者之间的任何内容引导时单引号和冒号"。

为了更清楚我上面的意思:

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'!

我希望你看到,我的问题是什么。任何有用的帮助将不胜感激。

提前谢谢,

问候英格玛

【问题讨论】:

  • 您应该告诉生成此文本的人,以弄清楚如何发出正确的 JSON 文本,而不是自己编造。
  • @chris85 它看起来像一些混蛋的自制 JSON。对于初学者来说,标识符没有被引用,这使得它不是 JSON。引用也中断了。
  • 在定义一些结构之前真的没有解决方案。最大的问题是字符串内部没有分隔符引用。例如,这很容易成为有效的 'This ' asdf ' "asdf" field is required!', dateISO : 'This is a test...'
  • @miken32 我们中的一个人误解了 stackoverflow 的精神。我也没有问这是否是有效的 JSON,我也不相信它是有效的 JSON。这是旧版本 jqValidate 的专有元数据,事实上我现在必须使用它。感谢您的 - 如果不是很有帮助 - 回复。
  • 你是一个带着西西弗斯式的任务来到这里的人,希望有人花时间为你解决它!这不是一件容易的事(可能吗?),如果可能的话,修改数据源将更有可能成功。您有生成数据的程序的源代码链接吗?

标签: php regex


【解决方案1】:

这应该工作:

正则表达式:

/(?<=required)\s:\s+\'(.+)\',|(?<=dateISO)\s:\s+\'(.+)\'/g

输入:

{ messages : { required :   'This 'asdf' "asdf" field is required!', dateISO : 'This is a test...' } , rules : { dateISO : true , required : true } }

输出:

This 'asdf' "asdf" field is required!
This is a test...

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);
?>

见:https://regex101.com/r/UD7pgq/2

【讨论】:

  • 感谢您的建设性意见。两个问题: 1. required 和 dateISO 只是众多标志中的两个。此标志因情况而异。 2. 输入字符串中的空白仅代表这样一个事实,即人工输入可能出于何种原因充满了无用的空格。我的目标是删除所有空格,但在单引号之间(在消息中)。如果我只是简单地杀死所有空格,则消息将如下所示:Thisisatest... 或其他任何内容。感谢您的热心帮助。
【解决方案2】:

对于那些对我自己和将来自己制定的解决方案感兴趣的人,我是这样解决的:

给定字符串是(*在您评论引号中未转义的引号之前,请阅读我帖子的结尾!):

{ messages : { required : 'This 'asdf' "asdf" field is required!', dateISO : 'This is a test...' } , rules : { dateISO : true , required : true } }


忘记这个吧!!!我自欺欺人,根本不需要第 1 点,因为(在 jqValidate 的情况下)没有其他部分存在,如“消息”,让 DEV 定义 TEXT-IN-QUOTES !!!直接跳到第 2 点!!!

但当然基本原则仍然存在。

/*

1。仅从整个修剪后的字符串中获取消息:

^(?:\{\s*messages\s*:\s*\{){1}(.*)(?:\}\s*,(?:.*)\}){1}$

preg_match 的结果数组将包含要在 index1 上使用的字符串:

required : 'This 'asdf' "asdf" field is required!', dateISO : 'This is a test...'

*/


  1. 勾勒出字符串的相应消息(魔法来了):

:\s*\'((?:(?!\'\s*,(?:.*):\s*\'(?:.*)).)+)\'

preg_match_all 的结果数组现在将包含 ALL 子数组中带有 index1 的消息,你可以做任何你想做的事情(干净,用 ' 替换 ",trim(),addlashes( )、重建它、替换数据库中的错误条目等...)。

(*) 顺便说一句:在这个系统中,管理员用户(不是开发人员)已经向数据库写入了数千条 jqValidate 规则和消息,但系统中没有对此类错误条目进行验证。在我手动搜索和清理所有这些条目或让管理员用户重写他们多年的工作之前,我选择通过一个带有正则表达式的小脚本来清理它。 这就是正则表达式的用途......至少,我理解的方式。

如果您对它的外观感兴趣:regex101.com

并且:是的,您可以在 jqValidate 中使用单引号 - 只要您将其转义(例如通过addslashes()):

【讨论】:

    猜你喜欢
    • 2012-11-30
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多