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

报告相同问题?

悬赏问题

  • ¥15 三菱FX系列PLC串口指令
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3