2012-02-02 2 views

ответ

3

Вот разбивка регулярных выражений:

(     # start a capturing group (1) 
    [0-9a-zA-Z-]+  # one or more digits or letters or hyphens 
    (?:    # start a non-capturing group 
     '    # a literal single quote character 
     [a-zA-Z0-9-]+ # one or more digits or letters or hyphens 
    )*     # repeat non-capturing group zero or more times 
)      # end of capturing group 1 

Регулярное выражение в виде /.../g и в цикле while, что означает, что код внутри while будет выполняться для каждого совпадающего совпадения регулярного выражения.

+2

В обоих случаях, класс персонажа «один или более цифр или букв или дефис». –

+0

@ davorg - Спасибо, я не заметил, что хвост '-'. –

2

Ответ F.J - идеальное нарушение. Но ... он оставил важную часть, которая является/g в конце. Он сообщает синтаксическому анализатору, чтобы он продолжался там, где он был остановлен с последнего раза. Таким образом, цикл while будет продолжать циклически перебирать строку до тех пор, пока не получит точку, в которой нет других совпадений.

3

Там есть инструмент для этого: YAPE::Regex::Explain

The regular expression: 

(?-imsx:([0-9a-zA-Z\-]+(?:'[a-zA-Z0-9\-]+)*)) 

matches as follows: 

NODE      EXPLANATION 
---------------------------------------------------------------------- 
(?-imsx:     group, but do not capture (case-sensitive) 
         (with^and $ matching normally) (with . not 
         matching \n) (matching whitespace and # 
         normally): 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    [0-9a-zA-Z\-]+   any character of: '0' to '9', 'a' to 
          'z', 'A' to 'Z', '\-' (1 or more times 
          (matching the most amount possible)) 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more 
          times (matching the most amount 
          possible)): 
---------------------------------------------------------------------- 
     '      '\'' 
---------------------------------------------------------------------- 
     [a-zA-Z0-9\-]+   any character of: 'a' to 'z', 'A' to 
           'Z', '0' to '9', '\-' (1 or more times 
           (matching the most amount possible)) 
---------------------------------------------------------------------- 
    )*      end of grouping 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 
Смежные вопросы