Regular expresion question

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Regular expresion question

Post by klevis miho »

Look at those lines:
- scarface (teletext pag. 760) - 777
- Total Tv: the beginning - 777
- nobody's land
- national geo - pandas (teletext pag. 528)

how can I do a regular expression on those lines to get only the program name, respectively:
scarface
Total Tv: the beginning
nobody's land
national geo - pandas

?

Any help would be appreciated
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Regular expresion question

Post by pickle »

We have a forum just for regular expressions. I've moved this thread there.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Regular expresion question

Post by AbraCadaver »

klevis miho wrote:Look at those lines:
- scarface (teletext pag. 760) - 777
- Total Tv: the beginning - 777
- nobody's land
- national geo - pandas (teletext pag. 528)

how can I do a regular expression on those lines to get only the program name, respectively:
scarface
Total Tv: the beginning
nobody's land
national geo - pandas

?

Any help would be appreciated
Assuming that is the full range of possible variations, this might work:

Code: Select all

if(preg_match('/- ([^(:-]+)/', $line, $title)) {
    $title = trim($title[1]);
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Regular expresion question

Post by klevis miho »

Thnx it worked.
But the last part:
- national geo - pandas (teletext pag. 528)

Didn't display:
national geo - pandas

Any suggestion?
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Regular expresion question

Post by klevis miho »

This is the longest text i need to do a regex:

- Mtv commercial - abu & quiz: the knight rider (teletext pag. 528) - 777 - 778
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Regular expresion question

Post by AbraCadaver »

Yeah the text really isn't predictable enough. This is probably the best you'll do and assumes no numbers in the title. Maybe some else can provide a better one:

Code: Select all

if(preg_match('/- ([^(\d]+)/', $line, $title)) {
    $title = trim(str_replace('-', '', $title[1]));
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Regular expresion question

Post by klevis miho »

Yeah this does this string but it doesnt do this:

MTV 1 K.A.R. - 777

:D

this regular expressions are driving me crazy man
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Regular expresion question

Post by AbraCadaver »

If the text is not predictable, at least somewhat, then a regex will never give you exactly what you want.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Regular expresion question

Post by klevis miho »

I posted a somewhat predictable text in a challenge here :D
Post Reply