LMW-HH
LMW-HH

Reputation: 1193

How to get list of strings from string using a pattern?

I know the question sound's a bit weird. Let me explain the situation:

I have an undefined text that looks like this:

Lorem {placeholder1} ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. {placeholder2} Cum sociis natoque penatibus et magnis dis parturient montes, {placeholder3} nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo

As you may notice, there are some placeholders inside the text: {PlaceholderX}. The only thing I know is that a placeholder is surrounded by {}. I dont know what's between them.

Now i'm looking for the best way to get a list of all strings surrounded by {} out of my text.

Or, to make it more general, is there a method where I can provide some kind of pattern like {*} and get back all fitting words as strings?

Upvotes: 1

Views: 3094

Answers (7)

code4life
code4life

Reputation: 15794

Use a lazy-plus regex pattern. This will work:

var txt =
    @"Lorem {placeholder1} ipsum dolor sit amet, consectetuer adipiscing elit. 
    Aenean commodo ligula eget dolor. Aenean massa. {placeholder2} Cum sociis 
    natoque penatibus et magnis dis parturient montes, {placeholder3} nascetur 
    ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium 
    quis, sem. Nulla consequat massa quis enim. Donec pede justo";

// need to do a lazy plus match...
var pattern = @"\{.+?\}";
var matches = Regex.Matches(txt, pattern);
foreach(Match match in matches)
{
    Console.WriteLine(match.Value);
}

The output will be

{placeholder1}
{placeholder2}
{placeholder3}

This being said, have you looked into NVelocity?

Upvotes: 0

Justin Pihony
Justin Pihony

Reputation: 67075

You are looking for regular expressions, in this case you need to make use of lookarounds

(?<=\{)(.*)(?=\})

The .* means it will find any non-space character between the braces

Here is a C# tutorial on how this can be used

Here is an example that shows how to pull out each item

I have adapted it for your example

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
    // First we see the input string.
    string input = "Lorem {placeholder} ipsum {placeholder2} ...";

    // Here we call Regex.Match.
    Match match = Regex.Match(input, @"(?<=\{)(.*)(?=\})",
        RegexOptions.IgnoreCase);

    // Here we check the Match instance.
    if (match.Success)
    {
        // Finally, we get the Group value and display it.
        foreach(var matchgroup in match.Groups)
            Console.WriteLine(matchgroup.Value);
    }
    }
}

Upvotes: 4

daryal
daryal

Reputation: 14919

string s = "Lorem {placeholder1} ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. {placeholder2} Cum sociis natoque penatibus et magnis dis parturient montes, {placeholder3} nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo";
Regex regex = new Regex(@"{\w*}");
var temp= regex.Matches(s);
foreach(var item in temp)
{
     string key = item.ToString().Trim('{').Trim('}');
     Console.WriteLine(key);
}

Upvotes: 0

Rick Kuipers
Rick Kuipers

Reputation: 6617

You can use the following code, just paste in any regular expression from an answer posted. There are many possibilities depending on how the placeholders are formatted.

String s = "Lorem {placeholder1} ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. {placeholder2} Cum sociis natoque penatibus et magnis dis parturient montes, {placeholder3} nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo";
Regex r = new Regex("{[a-zA-Z0-9]+}"); // Or any other regex found in one of the answers.
MatchCollection mc = r.Matches(s);
foreach (Match m in mc) {
    Console.WriteLine(m.Value);
}

Make sure you're using

using System.Text.RegularExpressions;

Upvotes: 0

Guvante
Guvante

Reputation: 19203

Regex regex = new Regex("\{[^\}]+\}");
string[] matches = regex.Matches(text);

Upvotes: 1

Robbie
Robbie

Reputation: 19500

You could use a regular expression. Something like this:

string pattern = @"Your text with {placeholders} in it"
string[] placeholders = regex.Matches(input, @"\{\w+\}");

Upvotes: 1

vlad
vlad

Reputation: 4778

You're looking for Regular Expressions

Upvotes: 0

Related Questions