Get Text In Between Quotes

micropathic

Well-known member
Joined
Oct 23, 2003
Messages
75
Hi, I am reading a file to a string and I want to find the data between each set of quotes in the string. For instance, if my string looked something like this:

randomtext randomtext randomtext "this is what i want" randomtext randomtext "this is also what i want" randomtext

How could I get "this is what i want" and "this is also what i want" from the string?

Thanks for any help!
 
Alternative:

use the regex use the regex "([^"]*)"

PHP:
using System;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
	class Class1
	{
		[STAThread]
		static void Main(string[] args)
		{
			Console.Write("Enter the sentence to parse: ");
			string sentence = Console.ReadLine();
			StringCollection quotedText = new StringCollection();
			
			foreach(Match match in Regex.Matches(sentence, "\"([^\"]*)\""))
			{
				quotedText.Add(match.Result("$1"));
			}
			
			Console.WriteLine("\r\nThe following quoted text was found:");
			for(int i=1; i <= quotedText.Count; i++)
			{
				Console.WriteLine("{0}) {1}", i, quotedText[i-1]);
			}
		}
	}
}

And the output

Enter the sentence to parse: randomtext randomtext randomtext "this is what i wa
nt" randomtext randomtext "this is also what i want" randomtext

The following quoted text was found:
1) this is what i want
2) this is also what i want
Press any key to continue
 
Thanks for you help! Id prefer to use the string.split method if I could. I have a question about that though. How can I loop through the entire string until the end while getting all the text between the quotes? For instance, here is the an example that MS gives that I am trying to modify for my own use for string.split:


Code:
Dim delimStr As String = chr(34)
     Dim delimiter As Char() = delimStr.ToCharArray()
      Dim words As String = "one" &  chr(34) & "two" & chr(34) & "three" & chr(34) & "four"
     Dim sr As New System.IO.StreamReader("C:\MyTextFile.txt")
     Dim words As String = sr.ReadToEnd()
     Dim split As String() = Nothing
      
      Console.WriteLine("The delimiters are -{0}-", delimStr)
      Dim x As Integer
     
 For x = 1 To 5  THIS IS WHAT NEEDS TO BE MODIFIED, I NEED TO READ 
                 ALL INSTANCES, NOT JUST THE FIRST FIVE
         split = words.Split(delimiter, x)
         Console.WriteLine(ControlChars.Cr + "count = {0,2} ..............", x)
         Dim s As String
         For Each s In  split
            Console.WriteLine("-{0}-", s)
         Next s
      Next x
So, I guess my question is, what would X have to equal in order for me to ensure that the reading of string does not stop until it finds the final instance of text within quotes?
 
Actually though, I would not mind using a regular expression, but when I try to use the code above, I am getting the following errors:

-Expression expected
-Expression is not an array or a method, and cannot have an argument list.

The following are highlighted within the regular expression:
For Each match In Regex.Matches(sentence,"\"((^\"]*)\"")
Here is the code Im using:
Code:
Dim sr As New System.IO.StreamReader(SafeFileName)

        Dim sentence As String = sr.ReadToEnd()
        Dim quotedText As StringCollection = New StringCollection

        Dim match As Match
            For Each match In Regex.Matches(sentence,"\"((^\"]*)\"")
            quotedText.Add(match.Result("$1"))
        Next

        Dim i As Integer
        For i = 1 To quotedText.Count Step i + 1
            Console.WriteLine("{0}) {1}", i, quotedText(i - 1))
        Next
 
Back
Top