简体   繁体   English

Preg_match PHP —模式匹配

[英]Preg_match PHP — pattern matching

trying to understand preg_match, struggling to understand how to write and how to access what it has matched. 试图了解preg_match,努力了解如何编写以及如何访问已匹配的内容。 For example: 例如:

Every single movie name I have is in the format-- 我拥有的每个电影名称都采用以下格式:

MOVIE NAME (YEAR) 电影名称(年份)

eg Alice in Wonderland (2010) 例如《爱丽丝梦游仙境》(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) 几部电影在年末加上括号-例如,电影(其中之一)(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. 我也在迭代字符串数组-所以我基本上需要使用preg_match来达到\\([0-9]{4}\\)$$是行尾的标记吗?),那么该字符串的其余部分(不含该年份)以及两个变量。

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). 当我输入\\(时,它进入了(因为我没有两次转义。无论如何,非常感谢你们!您链接它的站点也很棒(帮助了数组问题,我没有意识到它使全字符串保持为0,因为好)。

well if your pattern is: SOMETHING + (YEAR) then your regex should be like this: 如果您的模式是:SOMETHING +(YEAR),那么您的正则表达式应如下所示:

 #^(.+)\((\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

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表示任何十进制数字:

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

This will make subgroup 1 contain the year. 这将使子组1包含年份。

This is one of the link where regex man be made 这是制作regex的链接之一

You also can use following for decimal number 您也可以使用以下十进制数字

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

这是一个在线正则表达式测试仪,在学习正则表达式并对其进行测试时非常有用: HiFi Regex Tester

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM