2012-04-02 10 views
10

walczę z budowania wyrażenia regularnego do analizowania tego rodzaju łańcuchów (Bible Pisma):PHP Format preg_match Biblia Pismo

'John 14:16–17, 25–26' 
    'John 14:16–17' 
    'John 14:16' 
    'John 14' 
    'John' 

Więc podstawowy wzór jest:

Book [[Chapter][:Verse]]

gdzie rozdział i werset są opcjonalne.

+0

Czy powinien pasować, nawet jeśli to tylko nazwa książki? Czy masz listę książek, które powinny pasować? W przeciwnym razie pasowałoby do każdego słowa. – JJJ

+0

Po prostu dopasuj dowolne słowo, prawdziwym problemem dla mnie jest posiadanie tak wielu opcjonalnych części. – Dziamid

Odpowiedz

4

Spróbuj tutaj

\b[a-zA-Z]+(?:\s+\d+)?(?::\d+(?:–\d+)?(?:,\s*\d+(?:–\d+)?)*)? 

Zobacz i przetestować go here on Regexr

powodu (?:,\s*\d+(?:–\d+)?)* w końcu można mieć listę wierszy, wiersze waha się na końcu.

+0

Twoja jest najbardziej ogólna. Dodałem tylko "[-]" zamiast łącznika jako sugerowanego przez @Robby i kilka uchwyconych nawiasów, aby było idealne. – Dziamid

3

Użyj tego regex:

[A-Za-z]+(([0-9]+)(:[0-9]+)?([\-–][0-9]+)?(, [0-9]+[\-–][0-9]+)?)? 

lub w jego '' ładniejszej wersji:

\w+((\d+)(:\d+)?([\-–]\d+)?(, \d+[\-–]\d+)?)? 

AKTUALIZACJA: pasujące do kresek lub myślników


UWAGA: Przetestowałem to i pasuje do WSZYSTKICH 5 możliwych wersji.

Przykład: http://regexr.com?30h4q

enter image description here

9

myślę, że robi to, co trzeba:

\w+\s?(\d{1,2})?(:\d{1,2})?([-–]\d{1,2})?(,\s\d{1,2}[-–]\d{1,2})? 

założeniach:

  • Liczby są zawsze w zestawach 1 lub 2 cyfry
  • Dziarskość dopasuje albo następnego - i

Poniżej regex z komentarzem:

" 
\w   # Match a single character that is a “word character” (letters, digits, and underscores) 
    +   # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
\s   # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) 
    ?   # Between zero and one times, as many times as possible, giving back as needed (greedy) 
(   # Match the regular expression below and capture its match into backreference number 1 
    \d   # Match a single digit 0..9 
     {1,2}  # Between one and 2 times, as many times as possible, giving back as needed (greedy) 
)?   # Between zero and one times, as many times as possible, giving back as needed (greedy) 
(   # Match the regular expression below and capture its match into backreference number 2 
    :   # Match the character “:” literally 
    \d   # Match a single digit 0..9 
     {1,2}  # Between one and 2 times, as many times as possible, giving back as needed (greedy) 
)?   # Between zero and one times, as many times as possible, giving back as needed (greedy) 
(   # Match the regular expression below and capture its match into backreference number 3 
    [-–]  # Match a single character present in the list “-–” 
    \d   # Match a single digit 0..9 
     {1,2}  # Between one and 2 times, as many times as possible, giving back as needed (greedy) 
)?   # Between zero and one times, as many times as possible, giving back as needed (greedy) 
(   # Match the regular expression below and capture its match into backreference number 4 
    ,   # Match the character “,” literally 
    \s   # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) 
    \d   # Match a single digit 0..9 
     {1,2}  # Between one and 2 times, as many times as possible, giving back as needed (greedy) 
    [-–]  # Match a single character present in the list “-–” 
    \d   # Match a single digit 0..9 
     {1,2}  # Between one and 2 times, as many times as possible, giving back as needed (greedy) 
)?   # Between zero and one times, as many times as possible, giving back as needed (greedy) 
" 

A oto kilka przykładów jego użycia w PHP:

if (preg_match('/\w+\s?(\d{1,2})?(:\d{1,2})?([-–]\d{1,2})?(,\s\d{1,2}[-–]\d{1,2})?/', $subject)) { 
    # Successful match 
} else { 
    # Match attempt failed 
} 

Uzyskaj tablicę wszystkich dopasowań w danym ciągu znaków:

preg_match_all('/\w+\s?(\d{1,2})?(:\d{1,2})?([-–]\d{1,2})?(,\s\d{1,2}[-–]\d{1,2})?/', $subject, $result, PREG_PATTERN_ORDER); 
$result = $result[0]; 
+0

Czy pasuje do myślnika lub myślnika? – Dziamid

+0

tak, czy to prawda? – Robbie

+0

+1 za to, dziękuję – Dziamid

0
([1|2|3]?([i|I]+)?(\s?)\w+(\s+?))((\d+)?(,?)(\s?)(\d+))+(:?)((\d+)?([\-–]\d+)?(,(\s?)\d+[\-–]\d+)?)? 

działa dla prawie każdej książki ...

0
(\b[a-zA-Z]\w+\s\d+)(:\d+)+([-–]\d+)?([,;](\s)?(\d+:)?\d+([-–]\d+)?)? 

To jest hybryda wszystkich przedstawionych tutaj kodów. Jedyne formaty nie podświetli są „nazwa książka tylko” lub „książka & rozdział tylko” (wystarczy dodać „: 1 wszystko” po Chapter #) znalazłem kody podane kwalifikować zbyt wiele odmian , niezgodne ze składnią wiersza Biblii.

Są to przykłady testowałem w RegExr: (nie można jeszcze dodawać zdjęcia)

John Humbolt 14: 16-17, 25-26
Jana 14: 16-17
Jan 14:16
Jan 77: 3; 2: 9-11
Jana 5: 1 wszystko Brad 555-783-6867
John 6
Cześć, jak się masz
Ezra 32: 5Jana 14 : 16-17, 25-36
23:34
Jana 14: 16-17,25-36
Jan 14: 16-17; 32:25

Powiązane problemy