0

trying to understand preg_match, struggling to understand how to write and how to access what it has matched. For example:

Every single movie name I have is in the format--

MOVIE NAME (YEAR)

e.g. Alice in Wonderland (2010)

I want to be able to get the movie title into a different variable from the string.

A few movies have parentheses outside of the year -- as in, The movie (Has One of These) (2008)

I'm iterating over an array of strings as well -- so I basically need to use preg_match to get to \([0-9]{4}\)$ (is $ the mark of an end of the line?) and then the rest of the string without that year as well in two variables.

Can anyone possibly help?

EDIT: Huh. I swear I typed \ . When I type \( it went into ( because I didn't double escape. Anyway, thank you guys very much! The site you linked it also awesome (helped with array problems, I didn't realize it kept full string at 0 as well).

6
  • 1
    It seems like you're almost there. What is the problem you're having exactly, though? What do you want help with exactly? Commented Dec 8, 2011 at 10:20
  • I guess I really don't understand how to skip over everything at the beginning in terms of regex terms Commented Dec 8, 2011 at 10:23
  • Well, you kind of have with the regex you used - the only difference is that you'd need to escape the ()s. ie: ([0-9]{4})$ should match the (nnnn) year at the end of a movie title. Commented Dec 8, 2011 at 10:35
  • @Paul did any of the answers helped you, if so. upvote them and check as correct the answer you think was more helpful. Commented Dec 8, 2011 at 11:00
  • I am unable to because i have 14, not 15 reputation... Commented Dec 8, 2011 at 11:06

4 Answers 4

3

well if your pattern is: SOMETHING + (YEAR) then your regex should be like this:

 #^(.+)\((\d{4})\)$#

Explanation:

 # -> pattern delimiter
 ^ -> beginning of string
 (.+) -> any character "." once or more "+"
 \( -> escape parenthesis character
 \d{4} -> four digits
 \) -> escape parenthesis character
 $ -> end of string

Example

Sign up to request clarification or add additional context in comments.

Comments

1

Looking for patterns in these lines:

Alice in Wonderland (2010)
The movie (Has One of These) (2008)

You suggested in your question to use the following regular expression:

([0-9]{4})$

to match the year at the end of the line. $ is infact a marker for the end of the line, however, the ) is a special character in a regular expression that needs to be slashed to work:

\(([0-9]{4})\)$
  ^         ^^ both brackets have been slashed to match them literally
  '- subgroup parenthesis.

or by using \d for any decimal number:

\((\d{4})\)$

This will make subgroup 1 contain the year.

Comments

0

This is one of the link where regex man be made

You also can use following for decimal number

\((\d{4})\)$

Comments

0

Here's an online regexp tester, it is very useful when learning regular expressions and testing them: HiFi Regex Tester.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.