C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-15-2009, 11:59 AM   #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.
drygnet is offline   Reply With Quote
Old 10-15-2009, 12:40 PM   #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   Reply With Quote
Old 10-15-2009, 01:15 PM   #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 :-)
drygnet is offline   Reply With Quote
Old 10-15-2009, 02:15 PM   #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   Reply With Quote
Old 10-15-2009, 02:57 PM   #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   Reply With Quote
Reply

Tags
regex

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:36 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22