micropathic Posted November 19, 2004 Posted November 19, 2004 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! Quote
Joe Mamma Posted November 19, 2004 Posted November 19, 2004 one easy way would to string.splt on " and use every other word from the resulting array. Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
HJB417 Posted November 19, 2004 Posted November 19, 2004 Alternative: use the regex use the regex "([^"]*)" 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 Quote
micropathic Posted November 19, 2004 Author Posted November 19, 2004 Thanks for you help! I'd 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: 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? Quote
micropathic Posted November 19, 2004 Author Posted November 19, 2004 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 I'm using: 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 Quote
HJB417 Posted November 20, 2004 Posted November 20, 2004 An error in translation For Each match In Regex.Matches(sentence, """([^""]*)""") C# Language Specification - 2.4.4.5 String literals Visual Basic Language Concepts - Strings If you're not gonna read both, at least read the vb one. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.