I have four types of strings, as shown below. The difference is that the two numbers (23) and (15) may be there in string or not be there.
(23) Abc 11 (15) Krtss 14 (25:05 IN 2ND)
Abc 11 (15) Krtss 14 (25:05 IN 2ND)
(23) Abc 11 Krtss 14 (25:05 IN 2ND)
Abc 11 Krtss 14 (25:05 IN 2ND)
The Text Abc and Krtss can be any two names. These names may even contain spaces or hyphens.
I need:
- A single regular expression to extract “Abc” from all 4 strings.
- A second single regular expression to extract “Krtss” from all 4 strings.
Here Abc and Krtss are two names and may contain spaces and hyphens also.
If a single regex is not possible, please give me a small PHP code snippet to get the desired output.
If all you want is a simple answer to get the two names, then do this:
Note that for the first name I’m enforcing that it start with a letter, so as to avoid accidentally eating up the space after the first parenthesized number, if it is present. If that isn’t a problem, you can make the first part of the regex inside parens the same as the second one.