user3394486
user3394486

Reputation: 89

How to pass values from a string array into a Regex in C#

I am currently writing a program that will check a line from a file and see if a US state is contained in that line and that it is also spelled correctly. I have it currently working for a single state. I would like to be able to see if any of all the US states are in the line.

This is my code so far below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication3
{
class Program
{
    static void Main(string[] args)
    {

        var r = new Regex(@"\bArizona\b", RegexOptions.IgnoreCase);
        string[] lines = File.ReadAllLines(@"C:\sampledata.dat");
        foreach (string s in lines)
        {
            var m = r.Match(s);
            Console.WriteLine(m.Success); // false
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }
}
}

Essentially I would like to do something like var r = new Regex(@"\bAll US States.txt\b", RegexOptions.IgnoreCase);

Upvotes: 2

Views: 1760

Answers (2)

Rion Williams
Rion Williams

Reputation: 76547

If you wanted to see if any of the states were contained, you could essentially use the string.Join() method to generate an expression that would match any of them:

// Read your lines
var states = File.ReadAllLines(@"C:\states.txt");

// Build a pipe delimited string (e.g. State1|State2|State3 ...) to use an a Regex with necessary boundaries
// leading and trailing boundaries
var pattern = $@"\b{(string.Join("\b|\b", states))}\b";

// Now use that pattern to build a Regex to check against (using C# string interpolation)
var regex = new Regex(pattern, RegexOptions.IgnoreCase);

// Now loop through your data here and check each line
string[] lines = File.ReadAllLines(@"C:\sampledata.dat");
foreach (string s in lines)
{
        var m = regex.Match(s);
        Console.WriteLine(m.Success); // false
        Console.WriteLine(s);
        Console.ReadLine();
}

Additionally, if you aren't able to use string interpolation to build your pattern, simply use the older string.Format() approach:

var pattern = string.Format("\b{0}\b", string.Join("\b|\b", states));

Upvotes: 3

Dmitry Egorov
Dmitry Egorov

Reputation: 9650

The regex you're looking for is

\b(?:Alabama|Alaska|Arizona|...)\b

To compose it from a list stored in an All US States.txt file use File.ReadAllLines:

var states = File.ReadAllLines(@"All US States.txt");
var r = new Regex(string.Format(@"\b(?:{0})\b", string.Join("|", states)), RegexOptions.IgnoreCase);

Upvotes: 2

Related Questions