php
  • regex
  • 2013-04-22 19 views -3 likes 
    -3

    拆分字符串我有以下字符串:如何使用正則表達式

    Str=" $str='The requirements of this chapter apply to the following: 
           (a) New buildings or portions thereof used as health care occupancies (see 1.4.1) (b) Additions made to, or used as, a health care occupancy (see 4.6.6 and 18.1.1.4) Exception: The requirement of 18.1.1.1.1 shall not apply to additions classified as occupancies other than health care that are separated from the health care occupancy in accordance with 18.1.2.1(2) and conform to the requirements for the specific occupancy in accordance with Chapters 12 through 17 and Chapters 20 through 42, as appropriate. (c) Alterations, modernizations, or renovations of existing health care occupancies (see 4.6.7 and 18.1.1.4) (d) Existing buildings or portions thereof upon change of occupancy to a health care occupancy (see 4.6.11) Exception*: Facilities where the authority having jurisdiction has determined equivalent safety has been provided in accordance with Section 1.5.'; 
         "; 
    

    ,但我我想要的outut是這樣的: -

    $str='The requirements of this chapter apply to the following: 
    (a) New buildings or portions thereof used as health care occupancies (see 1.4.1) 
    
    (b) Additions made to, or used as, a health care occupancy (see 4.6.6 and 18.1.1.4) Exception: The requirement of 18.1.1.1.1 shall not apply to additions classified as occupancies other than health care that are separated from the health care occupancy in accordance with 18.1.2.1(2) and conform to the requirements for the specific occupancy in accordance with Chapters 12 through 17 and Chapters 20 through 42, as appropriate. 
    
    (c) Alterations, modernizations, or renovations of existing health care occupancies (see 4.6.7 and 18.1.1.4) 
    
    (d) Existing buildings or portions thereof upon change of occupancy to a health care occupancy (see 4.6.11) Exception*: Facilities where the authority having jurisdiction has determined equivalent safety has been provided in accordance with Section 1.5.'; 
    

    我要拆分的條件(AZ )。如何使用正則表達式來做到這一點?

    感謝

    +0

    你能重新檢查你的代碼在問題中給出的格式是否符合您的要求?這並不是完全明顯,如果這是你的意圖。 – jsalonen 2013-04-22 12:11:35

    +1

    你想用什麼語言來做到這一點? PHP還是Javascript? – MaxArt 2013-04-22 12:11:44

    +0

    人,$ str是有效的javascript變量名... – codefreak 2013-04-22 12:17:45

    回答

    0

    對於一個字符集,你可以使用分別與[a-z][A-Z]較低和大寫字母。你需要的不是簡單的字母,而是分割(<letter>)(括號括起來)。爲此,您可能需要\([a-z]\)(也就是說,要將括號轉義,因爲它們被稱爲分離表達式組的元字符)。

    0

    您可以使用正則表達式:

    str = "The requirements of this chapter apply to the following:(a)hello (b)asdadsdsa (c)asdadssad"; 
    $str = str.replace(/(\([a-z]+\))/ig, "<br/>$1"); 
    /* 
    $str's value now is: 
    The requirements of this chapter apply to the following: 
    <br/>(a)hello 
    <br/>(b)asdadsdsa 
    <br/>(c)asdadssad 
    */ 
    
    0

    不清楚自己想要什麼,但使用了一枚正則表達式替換應該讓你開始:

    $str.replace(/(\([a-z]+\))/ig, '\n$1'); 
    
    相關問題