Page 1 of 1
Regular expresion question
Posted: Wed Dec 16, 2009 9:28 am
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
Re: Regular expresion question
Posted: Wed Dec 16, 2009 10:25 am
by pickle
We have a forum just for regular expressions. I've moved this thread there.
Re: Regular expresion question
Posted: Wed Dec 16, 2009 10:33 am
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]);
}
Re: Regular expresion question
Posted: Thu Dec 17, 2009 4:35 am
by klevis miho
Thnx it worked.
But the last part:
- national geo - pandas (teletext pag. 528)
Didn't display:
national geo - pandas
Any suggestion?
Re: Regular expresion question
Posted: Thu Dec 17, 2009 8:34 am
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
Re: Regular expresion question
Posted: Thu Dec 17, 2009 9:55 am
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]));
}
Re: Regular expresion question
Posted: Thu Dec 17, 2009 10:02 am
by klevis miho
Yeah this does this string but it doesnt do this:
MTV 1 K.A.R. - 777
this regular expressions are driving me crazy man
Re: Regular expresion question
Posted: Thu Dec 17, 2009 10:45 am
by AbraCadaver
If the text is not predictable, at least somewhat, then a regex will never give you exactly what you want.
Re: Regular expresion question
Posted: Thu Dec 17, 2009 10:51 am
by klevis miho
I posted a somewhat predictable text in a challenge here
