Dzi*_*mid 10 php regex preg-match
我正在努力构建一个正则表达式来解析这种字符串(圣经经文):
'John 14:16–17, 25–26'
'John 14:16–17'
'John 14:16'
'John 14'
'John'
Run Code Online (Sandbox Code Playgroud)
所以基本模式是:
Book [[Chapter][:Verse]]
章和节是可选的.
我认为这可以满足您的需求:
\w+\s?(\d{1,2})?(:\d{1,2})?([-–]\d{1,2})?(,\s\d{1,2}[-–]\d{1,2})?
Run Code Online (Sandbox Code Playgroud)
假设:
-和–以下是带注释的正则表达式:
"
\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)
"
Run Code Online (Sandbox Code Playgroud)
以下是它在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
}
Run Code Online (Sandbox Code Playgroud)
获取给定字符串中所有匹配项的数组
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];
Run Code Online (Sandbox Code Playgroud)
在这里试试这个
\n\n\\b[a-zA-Z]+(?:\\s+\\d+)?(?::\\d+(?:\xe2\x80\x93\\d+)?(?:,\\s*\\d+(?:\xe2\x80\x93\\d+)?)*)?\nRun Code Online (Sandbox Code Playgroud)\n\n在 Regexr 上查看并测试它
\n\n因为(?:,\\s*\\d+(?:\xe2\x80\x93\\d+)?)*最后你可以有一个诗句列表,最后的诗句范围。
| 归档时间: |
|
| 查看次数: |
2470 次 |
| 最近记录: |