Stupid regex help

  • Thread starter Thread starter Austin Ehlers
  • Start date Start date
A

Austin Ehlers

Happy new years everybody!
Alright, I've got a string, with this as an example:

Title: The title of something

Title may or may not be followed with a colon (:), dash (-), or a
space-dash ( -)
I need to extract "The title of something". Here's what I got so far:

^(Title)[:\- ]|( \-)

which, I think, will match all possible varieties that I need. But
how do I extract whatever is to the right of it?

Thanks,
Austin Ehlers
 
If you use this... (?<=(Title))([\:|-]\s*)(.*) the third group will
contain the text you want. I used the "." assuming that your text in the
title could include any character other than a new line. I think it should
probably have a $ at the end to demark the end of the line but I'm not sure
how your string is coming in.

HTH,

Bill
 
If there are only three options those being (:/-/ -)
Then this might be the solution;

foreach(string myString in myCollectionOfTheseStrings)
{
int charCtr = 0;
char prevChar = '';
string begin = "";
string end = "";
foreach(char c in myString)
{
if(c == ':')
{
begin = myString.Substring(0,charCtr);
int cutPoint = charCtr+1;
end = myString.Substring(cutPoint, myString.Length -
cutPoint);
break;
}
else if(c == '-')
{
if(prevChar == '')
{
begin = myString.Substring(0,charCtr-1);
int cutPoint = charCtr+1;
end = myString.Substring(cutPoint, myString.Length -
cutPoint);
break;
}
else
{
begin = myString.Substring(0,charCtr);
int cutPoint = charCtr+1;
end = myString.Substring(cutPoint, myString.Length -
cutPoint);
break;
}
}
charCtr++;
prevChar = c;
}
// Do something with string begin and string end here
}

Of course this is presuming that none of the titles have
a ':' or '-' within the name, if that is the case my
solution sucks and you should ignore me.

Hope that helps
jax
 
Hello!

If you're supposed to do a simple string op, like checking for an ending
char, why not use the string.EndsWith() method? If this isn't what you're
looking for, let me know and I'll help you out with the RegEx (although your
posting was a little hard to understand).
 
Unfortunately, it looks like there will be many more diff.
combinations than I thought. For those that match my pattern, this
works:
(?<=(Title))((: )|( \- )|( )|(\-)|(\- ))(.*)+?$
and the last group.

Thanks for all the help everyone,
Austin

If you use this... (?<=(Title))([\:|-]\s*)(.*) the third group will
contain the text you want. I used the "." assuming that your text in the
title could include any character other than a new line. I think it should
probably have a $ at the end to demark the end of the line but I'm not sure
how your string is coming in.

HTH,

Bill
Austin Ehlers said:
Happy new years everybody!
Alright, I've got a string, with this as an example:

Title: The title of something

Title may or may not be followed with a colon (:), dash (-), or a
space-dash ( -)
I need to extract "The title of something". Here's what I got so far:

^(Title)[:\- ]|( \-)

which, I think, will match all possible varieties that I need. But
how do I extract whatever is to the right of it?

Thanks,
Austin Ehlers
 
Back
Top