php
  • regex
  • 2013-04-22 3 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.'; 
    

    Я хочу, чтобы разделить условие (аг). Как использовать регулярное выражение для этого?

    Благодаря

    +0

    Не могли бы вы перепроверить, что код дали в вопросе, отформатирован так, как вы хотели бы? Это не совсем очевидно, если это то, что вы намеревались сделать. – jsalonen

    +1

    На каком языке вы хотите его использовать? PHP или Javascript? – MaxArt

    +0

    people, $ str является действительным именем переменной javascript ... – codefreak

    ответ

    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'); 
    
    Смежные вопросы