doushi5117 2017-02-22 17:54
浏览 26
已采纳

需要PHP Regex帮助

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

This is the string to search (usually from 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

  • 写回答

2条回答 默认 最新

  • duanfu7994 2017-02-24 16:41
    关注

    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!!! Just jump over to point No. 2!!!

    But of course the basic principle remains.

    /*

    1. Get only the messages out of the whole trimmed string:

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

    The resulting array of preg_match will contain the string to work with on index1:

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

    */


    1. Tickle out the respective messages of the string (here comes the magic):

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

    The resulting array of preg_match_all will contain now ALL messages in sub-array with index1 and you can do whatever you want (clean, replace " with ', trim(), addslashes(), rebuild it, replace faulty entries in DB, etc...).

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

    And: Yes, you can use single quotes in jqValidate - as long as you escape it (e.g. by addslashes()):

    Single quotes in a jqValidate message

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料