2017-02-22 67 views
-1

我有一點幫助的請求,沒有那麼複雜,我想,但我無法弄清楚:PHP正則表達式的幫助需要

這是我的正則表達式:

/:\s*'([^:]*)'/g(我使用它沒有通過preg_match_all標誌克())

這是要搜索的字符串(通常從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'!

我希望你看,什麼是我的問題。任何有用的幫助將非常感激。

Thanx提前,

問候英格瑪

+6

你應該告訴誰在生成這個文本來弄清楚如何發出正確的JSON文本,而不是試圖自己編寫它。 – miken32

+0

@ chris85它看起來像一些混蛋自制的JSON。對於初學者來說,標識符沒有被引用,這使得它不是JSON。也打破引用。 – miken32

+1

直到你定義一些結構之前,真的沒有解決方案。最大的問題是在字符串內部沒有分隔符引用。例如,這可能很容易有效'This'asdf''「asdf」field is required!',dateISO:'這是一個測試...'' – sln

回答

0

對於那些你,誰感興趣的我在未來的時間制定出我自己併爲自己的解決方案,這裏是如何我解決了它:

給定的字符串是(*在您評論引號中未轉義的引號之前,請閱讀我發佈的結尾!):

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


忘記這個!我愚弄了我的自我,根本不需要第一點,因爲(在jqValidate的情況下)沒有其他部分像「消息」那樣存在,因此讓DEV定義文本輸入!跳到第2點!!!

但當然基本原則依然存在。

/*

1.獲取僅消息出了整個的修整字符串:

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

所得的preg_match的陣列將包含字符串與索引工作1

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

*/


  • 搔癢出字符串的各自的消息(這裏來的魔法):
  • :\s*\'((?:(?!\'\s*,(?:.*):\s*\'(?:.*)).)+)\'

    得到的preg_match_all數組現在將包含ALL子索引中的消息索引1,你可以做任何你想要的(乾淨的,用'替換',trim(),addslashes(),重建它,替換DB中的錯誤條目等等。 (*)BTW:在這個系統中,admin-users(而不是開發人員)已經向數據庫寫了成千上萬的jqValidate規則和消息,但是在系統中沒有像這樣的錯誤條目驗證。在我手動搜索並清除所有這些條目或讓管理員用戶重寫他們多年的工作之前,我選擇使用正則表達式的小小腳本來清理它。 這就是正則表達式的作用......至少,我理解的方式。

    如果您對它感興趣的樣子:regex101.com

    和:,您可以在jqValidate使用單引號 - 只要你逃避它(例如,通過和addslashes()):

    Single quotes in a jqValidate message

    0

    這應該工作:

    正則表達式:

    /(?<=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

    +0

    謝謝你的建設性意見。兩個問題:1.需要和日期ISO只是許多標誌中的兩個。這標誌着案件之間的變化。 2.輸入字符串中的空格僅代表事實,即人的輸入可能來自何種理由永遠充滿無用的空間。我的目標是刪除所有的空格,但刪除單引號之間的單詞(在消息中)。如果我只是簡單地殺死所有的空間,那麼信息就會看起來像:Thisisatest ...或者什麼。感謝您的熱心幫助。 –