Regular Expression Question

N

Nightcrawler

I am trying to parse out information using regular expressions. The
textfile I am using looks like this (I am only using 3 entires but in
reality the number could be anything):

#TIME:12:58
C:\Documents and Settings\me\My Documents\My Music\Artist - Title.mp3
#TIME:12:59
C:\Documents and Settings\me\My Documents\My Music\Awesome\Artist -
Title.mp3
#TIME:13:09
C:\Documents and Settings\me\My Documents\My Music\So Good\Yes\Artist
- Title.mp3

I need to parse out time (12:58 and 12:59 and 13:09) and the artist
and title part (artist - title bit NOT .mp3). I am having major issues
with the artist and title part. Can anyone please point me in the
right direction.

Any feedback is appreciated.

Thanks
 
N

not_a_commie

I haven't tested this but it should be close to what you want: "(?
<time>\d\d:\d\d)[^-]+\\(?<artist>[^-]+)\s*-\s*(?<title>.*?).mp3"

you want the [^-]+\\ to be greedy but not go past a hyphen. You also
don't want the artist to go past the hyphen. You want the title to be
not greedy.
 
N

Nicholas Paldino [.NET/C# MVP]

Nightcrawler,

Must you use regular expressions? The pattern looks pretty simple. You
have a line that starts with "#TIME:" after which is a time, and then
another line which is a path. You can use the static Path class in the
System.IO namespace to parse the filename from that line and then split it
on the hyphen to get the artist/title combination.
 
N

Nightcrawler

No, I don't have to. Would you recommend using your approach from an
efficiency/speed perspective?

Thanks
 
N

Nicholas Paldino [.NET/C# MVP]

I don't know if it is faster, but it would be much easier to maintain,
from my point of view. Regular Expressions are definitely a good thing, and
worth using, but usually overkill for simple patterns.
 
A

Arne Vajhøj

Nicholas said:
I don't know if it is faster, but it would be much easier to
maintain, from my point of view. Regular Expressions are definitely a
good thing, and worth using, but usually overkill for simple patterns.

One advantage of regex is that if the pattern used may need to be
extended later to more complex ones, then a regex is usually much easier
to extend than IndexOf and Substring.

Arne
 
J

Jon Skeet [C# MVP]

=?windows-1252?Q?Arne_Vajh=F8j?= said:
One advantage of regex is that if the pattern used may need to be
extended later to more complex ones, then a regex is usually much easier
to extend than IndexOf and Substring.

Hmm, not in my experience. Each time you maintain it, you have to make
sure you fully understand the exact nature of the pattern, and then
carefully change it, ensuring that you escape everything that needs to
be escaped etc.

If you're doing real *pattern* stuff which isn't easily expressed with
Contains/IndexOf etc, then regexes are appropriate - but otherwise I
find the explicit operations easier to understand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top