[英]PHP Regular expression parsing
I am having an array of strings. 我有一个字符串数组。
(102) Name3
How can I match for strings starting with (####) and get the Name part of the line easily. 如何匹配以(####)开头的字符串,并轻松获得该行的Name部分。 I am trying the following which is not working.
我正在尝试以下不起作用的方法。
if(preg_match("/(d+)/", $myArray[$i], $matches))
You need to escape the parentheses that are in the expected input string: 您需要转义期望输入字符串中的括号:
if(preg_match("/\((\d+)\) (.+)$/", $myArray[$i], $matches))
That's the following regular expression: 那是下面的正则表达式:
/
Start of regex (this can be any character, but is usually a /) /
正则表达式的开始(可以是任何字符,但通常是/) \\(
the character ( \\(
字符( (
begin capturing group 1 (
开始捕获第1组 \\d
any digit \\d
任何数字 +
previous match, 1 or more times +
上一场比赛,1次或更多次 )
end capturing group 1 )
结束捕获组1
a space (
begin capturing group 2 (
开始捕获第2组 .
almost any character +
previous match, 1 or more times +
上一场比赛,1次或更多次 )
end capturing group 2 )
结束捕获组2 $
end of string $
字符串结尾 /
End of regex (must match first character) /
正则表达式结尾(必须匹配第一个字符) You can find more descriptive definitions on regular-expressions.info . 您可以在regular-expressions.info上找到更多描述性定义。
With the above, matches will contain for example: 通过上述操作,匹配项将包含例如:
array(
'(123) input string',
123, // capturing group 1
'input string' // capturing group 2
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.