9 Replies - 1166 Views - Last Post: 20 May 2014 - 04:10 PM Rate Topic: -----

#1 Eowyn27   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 17-October 11

How do I match Regex if input was an array of strings?

Posted 19 May 2014 - 02:18 PM

In the MSDN, it gives an example using Regex.Matches method:
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "a*";
      string input = "abaabb";

      foreach (Match m in Regex.Matches(input, pattern)) 
         Console.WriteLine("'{0}' found at index {1}.", 
                           m.Value, m.Index);
   }
}
// The example displays the following output: 
//       'a' found at index 0. 
//       '' found at index 1. 
//       'aa' found at index 2. 
//       '' found at index 4. 
//       '' found at index 5. 
//       '' found at index 6.


I am wondering if string input was an array of strings instead, how would I use Regex.Matches?

What I was thinking about doing is having the input array set to ToString(), input.ToString(), but that doesn't seem to work.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I match Regex if input was an array of strings?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,835
  • Joined: 12-June 08

Re: How do I match Regex if input was an array of strings?

Posted 19 May 2014 - 02:21 PM

Typically you would apply the regex against each string in the collection.

If that is not what you are looking for then, exactly, how do you think applying a regex to an array would work?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7189
  • View blog
  • Posts: 24,365
  • Joined: 05-May 12

Re: How do I match Regex if input was an array of strings?

Posted 19 May 2014 - 03:26 PM

What was your expected behavior when you call ToString() on an array of strings? Did you verify that expectation?
Was This Post Helpful? 0
  • +
  • -

#4 Eowyn27   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 17-October 11

Re: How do I match Regex if input was an array of strings?

Posted 19 May 2014 - 03:52 PM

View PostSkydiver, on 19 May 2014 - 05:26 PM, said:

What was your expected behavior when you call ToString() on an array of strings? Did you verify that expectation?



I apologize if this thought process is completely idiotic/wrong but I expected if I converted the array of strings To String, it would append all the smaller strings to one giant string and have that giant string compare to Regex match.

I did verify it and nothing is returned/done. Seems like the Regex returned false.
Was This Post Helpful? 0
  • +
  • -

#5 Eowyn27   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 17-October 11

Re: How do I match Regex if input was an array of strings?

Posted 19 May 2014 - 05:51 PM

View Postmodi123_1, on 19 May 2014 - 04:21 PM, said:

Typically you would apply the regex against each string in the collection.

If that is not what you are looking for then, exactly, how do you think applying a regex to an array would work?


Good point. Can you give me an example?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7189
  • View blog
  • Posts: 24,365
  • Joined: 05-May 12

Re: How do I match Regex if input was an array of strings?

Posted 19 May 2014 - 08:10 PM

The following code prints out all the strings in an array. I bet you can figure out how to run each string through the regex.

string [] words = new string [] { "Hello", "World" };
foreach(string word in words)
    Console.WriteLine(word);



View PostEowyn27, on 19 May 2014 - 06:52 PM, said:

I did verify it and nothing is returned/done.

Really? I seem to get something... It's not quite what I wanted, but I do get something.
string[] words = new string[] { "Hello", "World" };
Console.WriteLine(words.ToString());


Was This Post Helpful? 0
  • +
  • -

#7 Eowyn27   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 17-October 11

Re: How do I match Regex if input was an array of strings?

Posted 20 May 2014 - 07:16 AM

Oh you mean you get System.String[] repeatedly... why does that happen?

This post has been edited by andrewsw: 20 May 2014 - 04:15 PM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,835
  • Joined: 12-June 08

Re: How do I match Regex if input was an array of strings?

Posted 20 May 2014 - 07:22 AM

... because the 'to string' is returning the type (since it does not know how to translate 'to string' into 'return everything in the array as one big string').
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How do I match Regex if input was an array of strings?

Posted 20 May 2014 - 02:45 PM

Basic principles of object oriented programming. Classes inherit methods from their parent classes. System.Object is the class from which all other classes inherit. Object defines a virtual method called ToString and provides a default implementation (which is to print the current object's Type name). Other classes may or may not override this method as they wish. A few do when it makes sense, most don't need to override .ToString().

Any time you see behavior like this that you don't understand, try searching it. Try googling "C# ToString". The first link:

http://msdn.microsof...t.tostring.aspx

Quote

The default implementation of the ToString method returns the fully qualified name of the type of the Object...


Not chastising you, just showing the useful and necessary resources for .NET programmers to know.
Was This Post Helpful? 0
  • +
  • -

#10 Eowyn27   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 150
  • Joined: 17-October 11

Re: How do I match Regex if input was an array of strings?

Posted 20 May 2014 - 04:10 PM

Thanks Curtis for the explanation! Made more sense and actually, I got Regex working now. Thanks everyone!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1