.NET Framework Developer Center > .NET Development Forums > Regular Expressions > How to read string end with question mark in regular expression?
Ask a questionAsk a question
 

AnswerHow to read string end with question mark in regular expression?

  • Monday, November 02, 2009 6:00 AMco coe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Monday, November 02, 2009 4:43 PMGregory Adam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    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 ?

    	//______________________________________________________________________
    		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();
    		}
    		//______________________________________________________________________
    
    Output
    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?
    

All Replies

  • Monday, November 02, 2009 7:21 AMGregory Adam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Try to escape those question marks, put a backslash before them

  • Monday, November 02, 2009 8:14 AMCocoe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.

  • Monday, November 02, 2009 8:47 AMGregory Adam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you explain what you're trying to do ?

    And also provide sample code - that would be helpful
  • Monday, November 02, 2009 9:30 AMco coe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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}");

    }

  • Monday, November 02, 2009 4:43 PMGregory Adam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    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 ?

    	//______________________________________________________________________
    		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();
    		}
    		//______________________________________________________________________
    
    Output
    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?
    
  • Wednesday, November 04, 2009 4:12 PMVQ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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*""");
    
  • Wednesday, November 11, 2009 8:59 AMeryangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.