Regex question

N

Nightcrawler

I have the following regex:

(?:(?<Title>.+)??(?<Remix>(?:\([^\)]+\)|\[[^\]]+\]))|(?<Title>.+))

This matches

title
title (remix)
title [remix]

How can I extend this to also match

title - remix

I also want the regex not to be sensitive to the amount of spacing
before and after the dash.

Thanks
 
J

Jeroen

Nightcrawler,

It's hard to help you with the actual regex, since your requirements
aren't completely clear to me. However, what often helps me with
regexes, is to split them across a few lines of code to improve
readability (use Stringbuilder or +=, according to your need for
speed).

Each line would have a part of the string you're trying to match. It
seems to me you need:

1. start of the string (?)
2. "title", exactly that string
3. 0...many whitespace characters
4. either a "-" or a "(" or a "[" character
5. "remix", exactly that string
6. optionally, either a ")" or a "]"
7. end of the string (?)

Perhaps the above will help you refine requirements, and then write
the regex yourself. I really suggest splitting the regex in your code
into separated parts as well, this greatly helps folks (including
yourself) read back a regular expression.

Regards,
Jeroen
 
E

Eps

Nightcrawler said:
I have the following regex:

(?:(?<Title>.+)??(?<Remix>(?:\([^\)]+\)|\[[^\]]+\]))|(?<Title>.+))

This matches

title
title (remix)
title [remix]

How can I extend this to also match

title - remix

I also want the regex not to be sensitive to the amount of spacing
before and after the dash.

Thanks

my advice to you is to find a regex tester and work it out yourself, an
example of a regex tester can be found below...

http://www.codeproject.com/KB/dotnet/expresso.aspx
 

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