Reputation: 9576
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. I am trying the following which is not working.
if(preg_match("/(d+)/", $myArray[$i], $matches))
Upvotes: 0
Views: 55
Reputation: 66170
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\d
any digit+
previous match, 1 or more times)
end capturing group 1
a space(
begin capturing group 2.
almost any character+
previous match, 1 or more times)
end capturing group 2$
end of string/
End of regex (must match first character)You can find more descriptive definitions on regular-expressions.info.
With the above, matches will contain for example:
array(
'(123) input string',
123, // capturing group 1
'input string' // capturing group 2
)
Upvotes: 4