.NET Framework Developer Center >
.NET Development Forums
>
Regular Expressions
>
How to read string end with question mark in regular expression?
How to read string end with question mark in regular expression?
- Hello,
I'm facing problem when I read string which is ended by question mark.
I use in my code as the following.
\"?(?<myfield>[^\"]?.*[^\"?]?)\"?
If string is ended by question mark, it's not working.
Please help me to solve my problem.
Thanks.
Co Coe
Answers
OK,
I have 2 possibilities for a csv column - have not tested the new line chars
(1) any sequence of chars not including a comma
(2) Anything between " and " including ""
What is the format if there are question marks involved ?Output//______________________________________________________________________ static void Main() { string csvColumn = @"(""(?<xxx>(([^""])|(""""))+)"")" // with double double quotes + @"|(?<xxx>[^,]+)" // not enclosed ; string pattern = "^" + csvColumn + "$"; string[] test = { @"abc", @"""de , f""", @""" a list with """" double """, @"""On a scale of 5, with 5 as “Best” and 1 as “Worst”, could your describe your rating? """, @"""On a scale of 5, with 5 as """"Best"""" and 1 as """"Worst"""", could your describe your rating? """ }; foreach ( string s in test ) { Match match = Regex.Match(s, pattern); if ( !match.Success ) Console.WriteLine("no match"); else Console.WriteLine(match.Result("${xxx}")); } Console.ReadLine(); } //______________________________________________________________________
abc de , f a list with "" double On a scale of 5, with 5 as "Best" and 1 as "Worst", could your describe your rating? On a scale of 5, with 5 as ""Best"" and 1 as ""Worst"", could your describe your rating?
- Marked As Answer byeryangMSFT, ModeratorThursday, November 12, 2009 6:25 AM
All Replies
- Try to escape those question marks, put a backslash before them
- Hi Adam,
Thanks for your reply. But the string I'm trying to capture is as the following.
"On a scale of 5, with 5 as “Best” and 1 as “Worst”, could your describe your rating? "
Currently I use the above expression to read it but fail to read.
- Can you explain what you're trying to do ?
And also provide sample code - that would be helpful - I'm trying to read csv file columns value from C#.net program.
I use regular expression to read columns values regarding to escape from comma, double quote, question mark because sentences come in various forms.
I need to read the whole sentence. Here is my regular expression code.
StringBuilder sb = new StringBuilder(sr.ReadLine());
Regex regEx = new Regex("(?<col1>.*),\"?(?<col2>[^\"]?.*[^\"]?)\"?,(?<col3>.*)", RegexOptions.RightToLeft);
if (regEx.Match(sb.ToString()).Success)
{
strcol1 = regEx.Replace(sb.ToString(), "${col1}");
strcol2 = regEx.Replace(sb.ToString(), "${col2}");
strcol3 = regEx.Replace(sb.ToString(), "${col3}");
}
OK,
I have 2 possibilities for a csv column - have not tested the new line chars
(1) any sequence of chars not including a comma
(2) Anything between " and " including ""
What is the format if there are question marks involved ?Output//______________________________________________________________________ static void Main() { string csvColumn = @"(""(?<xxx>(([^""])|(""""))+)"")" // with double double quotes + @"|(?<xxx>[^,]+)" // not enclosed ; string pattern = "^" + csvColumn + "$"; string[] test = { @"abc", @"""de , f""", @""" a list with """" double """, @"""On a scale of 5, with 5 as “Best” and 1 as “Worst”, could your describe your rating? """, @"""On a scale of 5, with 5 as """"Best"""" and 1 as """"Worst"""", could your describe your rating? """ }; foreach ( string s in test ) { Match match = Regex.Match(s, pattern); if ( !match.Success ) Console.WriteLine("no match"); else Console.WriteLine(match.Result("${xxx}")); } Console.ReadLine(); } //______________________________________________________________________
abc de , f a list with "" double On a scale of 5, with 5 as "Best" and 1 as "Worst", could your describe your rating? On a scale of 5, with 5 as ""Best"" and 1 as ""Worst"", could your describe your rating?
- Marked As Answer byeryangMSFT, ModeratorThursday, November 12, 2009 6:25 AM
- I'm not sure how your data is formated but according to sample you gave. You can use the following expression to extract the question.
"On a scale of 5, with 5 as “Best” and 1 as “Worst”, could your describe your rating? "
Regex.Matches(yourInput, @"""(.*\?)\s*"""); - Hi co coe,
it is a long time since the last reply, did you get useful information from above suggestions, or you still have any concern about this issue? Please let us know.
Thanks,
Eric
Please remember to mark helpful replies as answers and unmark them if they provide no help.


