Thread: regex to capture everything before string OR string

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Question regex to capture everything before string OR string

    Hi,

    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.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    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;
            }
        }
    }

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Quote Originally Posted by theoobe View Post
    I believe something like this should work...

    Code:
                Regex regex = new Regex("([a-zA-Z0-9\\.]+?).(xvid\\.dvdrip|dvdrip\\.xvid)", RegexOptions.IgnoreCase);
    Thank you for a very good answer, very kind to spend that much time.

    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 :-)

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    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;
            }
        }
    }
    :-)

  5. #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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

Tags for this Thread