0

I am trying to generate regular expression to find the following strings in text using preg_match or any other way. The pattern for me is everything between '(S' and 'E)'

(S02E01) (S12E02) (S10E03) (S01E04) (S07E05)

Is it possible to extract those strings?

Title example: "Coming soon, next Friday (S02E01) - Subscribe!"

devjs11
  • 1,898
  • 7
  • 43
  • 73
  • 2
    Yes, it's trivial. What problem are you having? Show the regular expressions you tried. – Barmar Nov 23 '13 at 00:56
  • @Barmar This is all I have echo preg_match('(^@)', $title); I pass through all titles with foreach and need to extract the pattern. – devjs11 Nov 23 '13 at 01:04
  • How is that supposed to do what you want? `^` matches the beginning of the line, and `@` matches that character. You also need to escape parentheses if you want to match them. – Barmar Nov 23 '13 at 01:05
  • And you're missing the delimiter characters that all the `preg_XXX` functions require around the regular expression. It doesn't seem like you've made any effort to learn how to write regular expressions or use them from PHP. – Barmar Nov 23 '13 at 01:07
  • @Barmar I understand and what is the case for (S..E..)? Title example: "Coming soon, next friday (S02E01) – devjs11 Nov 23 '13 at 01:08
  • You said _everything between '(S' and 'E)'_. But there is no `E)` in your string, it's `E01)`, `E02)`, etc. – Barmar Nov 23 '13 at 01:10

2 Answers2

2

Use this:

preg_match_all('/\(S\d+E\d+\)/', $string, $matches);

To print all the matches, do:

foreach ($matches[0] as $val) {
    echo $val;
}

Now go to regular-expressions.info to learn how regular expressions work.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Oh, thank you so much. I will learn more from there. Just one question. If I echo preg_match_all('/\(S\d+E\d+\)/', $string, $matches); it returns a number, how do I return a string? – devjs11 Nov 23 '13 at 01:17
  • The strings are in the `$matches` array. Did you try reading the documentation? – Barmar Nov 23 '13 at 01:18
  • yes, thank you. I do it by foreach ($matches as $val) {echo $val[0];} but I don't want to pass index. – devjs11 Nov 23 '13 at 01:22
  • You have to use an index, because `preg_match_all` set `$matches` to a two-dimensional array. This is because a regular expression can have capture groups, and the `$matches` needs a place for them. This regular expression doesn't use them, but more general regexps do. – Barmar Nov 23 '13 at 01:25
1

Yes it is possible using regex. However, I'd encourage trying without regex. PHP offers a lot of functions to deal with string manipulation. Regex tends to perform worse then the various str commands. With that being said sometimes it's not worth the development time to come up with an alternative to regex :). Luckily if your just searching between two strings there are a number of answers out there. Consider looking at this question > Get substring between two strings PHP

Community
  • 1
  • 1
Kirill Fuchs
  • 13,446
  • 4
  • 42
  • 72