example...
printf("helloworld");
i want to get the helloworld from this statement. using any regular expression? or any option?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
vishnu krishnanPosted Nov 6, 2010, 3:40 AM
vishnu krishnanPosted Nov 6, 2010, 3:01 AM
i have change that like
if (Regex.IsMatch(sample, "\"(.*?)\""))
{
Console.WriteLine(sample.ToString().Trim('"'));
}
got it but one problem, if am giving sample like hello"world" it will not working?
JurePosted Nov 4, 2010, 2:33 PM
"[^"]*"
or
"(.*?)"
but it will keep quotation marks, so you'll still have to trim the resulting strings. Also, don't forget to escape the quotes by writing \" instead of just ".
So, in C# this would be:
MatchCollection mc = Regex.Matches(sample, "\"(.*?)\"");
foreach (Match m in mc)
{
Console.WriteLine(m.ToString().Trim('"'));
}
All this is completely different from what I suggested first (splitting), regex won't split strings, but return just text that's between quotes.
vishnu krishnanPosted Nov 4, 2010, 12:54 PM
@"^('"')$";
this will give the error.
JurePosted Nov 4, 2010, 12:06 PM
name.split('"'), since you're going to use quotation mark(") as separator.
If you need more options, check out StringSplitter.
vishnu krishnanPosted Nov 4, 2010, 11:50 AM
string result= name.split( ? )
what will be the ( ? ) position
JurePosted Nov 4, 2010, 10:13 AM
The easiest way is to split the text with quotation marks as separators, using string.Split method. strings at every odd index in the returned array (result[1], result[3], etc) is what's between the quotes. If array's length is odd, then all quotations are between two quotation marks, thus properly formatted. If array's length is even, last quotation hasn't been ended/escaped with quotation mark yet.