![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 3
| I know this is possibly number 206:th question about regex on the internet in the last hour but... I'm having trouble with these kinds of strings: The Matrix.dvdrip.xvid and Shrek.xvid.dvdrip I'm trying to extract the title to get the results The.Matrix. and Shrek. The problem is that I (sort of) know what "tags" that can occur, but not in what order. I know that there are other (simple) solutions in C# but I'm trying to learn about regular expressions, and believe me I've learned a lot when trying to find a solution to this problem. Last edited by drygnet; 10-15-2009 at 12:02 PM. |
| drygnet is offline | |
| | #2 |
| Registered User Join Date: Mar 2009 Location: england
Posts: 76
| I believe something like this should work... Code: using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication5
{
class Program
{
static void Main()
{
String filename1 = "The.Matrix.dvdrip.xvid";
Console.WriteLine(filename1 + " = " + GetName(filename1));
String filename2 = "Shrek.xvid.dvdrip";
Console.WriteLine(filename2 + " = " + GetName(filename2));
Console.Read();
}
static String GetName(String text)
{
Regex regex = new Regex("([a-zA-Z0-9\\.]+?).(xvid\\.dvdrip|dvdrip\\.xvid)", RegexOptions.IgnoreCase);
Match match = regex.Match(text);
return match.Success ? match.Groups[1].Value : null;
}
}
}
|
| theoobe is offline | |
| | #3 | |
| Registered User Join Date: Oct 2009
Posts: 3
| Quote:
Now I feel stupid for not saying that I don't know how many tags that can occur. The input string could also be Lord.of.the.rings.pal.TS.divx So I need to cut the string at the first occurrence of any of the words: pal,divx,xvid,dvdrip,ntsc,hdtv, and so on... If its just stupid/impossible to do this with regex, please tell me and I'll make a loop :-) | |
| drygnet is offline | |
| | #4 |
| Registered User Join Date: Mar 2009 Location: england
Posts: 76
| Code: using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication5
{
class Program
{
static void Main()
{
String filename1 = "The.Matrix.dvdrip.xvid";
Console.WriteLine(filename1 + " = " + GetName(filename1));
String filename2 = "Shrek.xvid.dvdrip";
Console.WriteLine(filename2 + " = " + GetName(filename2));
String filename3 = "Lord.of.the.rings.pal.TS.divx";
Console.WriteLine(filename3 + " = " + GetName(filename3));
Console.Read();
}
static String GetName(String text)
{
Regex regex = new Regex("([a-zA-Z0-9\\.]+?).(pal|divx|xvid|dvdrip|ntsc|hdtv)", RegexOptions.IgnoreCase);
Match match = regex.Match(text);
return match.Success ? match.Groups[1].Value : null;
}
}
}
|
| theoobe is offline | |
| | #5 |
| Registered User Join Date: Oct 2009
Posts: 3
| Thank you! Super fast and exactly what I was looking for ! I suspected I needed a non-greedy qualifier, but I've tested them on the second group (pal|ntcs... I feel like I'm much closer to actually understanding regex now again, thank you! |
| drygnet is offline | |
![]() |
| Tags |
| regex |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| compare structures | lazyme | C++ Programming | 15 | 05-28-2009 02:40 AM |
| OOP Question DB Access Wrapper Classes | digioz | C# Programming | 2 | 09-07-2008 04:30 PM |
| String editor for a sentence inputted by a user - any suggestions or ideas? | the_newbug | C Programming | 4 | 03-03-2006 02:11 AM |
| Classes inheretance problem... | NANO | C++ Programming | 12 | 12-09-2002 03:23 PM |
| creating class, and linking files | JCK | C++ Programming | 12 | 12-08-2002 02:45 PM |