6

I am new to RegEx. I have a string like following. I want to get the values between [{# #}]

Ex: "Employee name is [{#John#}], works for [{#ABC Bank#}], [{#Houston#}]"

I would like to get the following values from the above string.

"John",
"ABC Bank",
"Houston"
7
  • 1
    You should take a look at How to Ask Commented Sep 3, 2017 at 23:53
  • What are "#"? Numbers? Anything? Commented Sep 3, 2017 at 23:54
  • 2
    It's not the best writing ever, but it looks pretty clear to me that he wants to extract values between hashes on strings that looks like: '[{# EXTRACT_THIS #}]' Regex might be one way of doing it Commented Sep 4, 2017 at 0:07
  • @anomeric No # is not number . Commented Sep 4, 2017 at 0:16
  • We have to know what # is. You can't expect anyone to write an expression to pull data from a string if we don't know what the rest of the string is Commented Sep 4, 2017 at 0:19

3 Answers 3

12

Based on the solution Regular Expression Groups in C#. You can try this:

       string sentence = "Employee name is [{#john#}], works for [{#ABC BANK#}], 
        [{#Houston#}]";
        string pattern = @"\[\{\#(.*?)\#\}\]";

        foreach (Match match in Regex.Matches(sentence, pattern))
        {
            if (match.Success && match.Groups.Count > 0)
            {
                var text = match.Groups[1].Value;
                Console.WriteLine(text);
            }
        }
        Console.ReadLine();
Sign up to request clarification or add additional context in comments.

1 Comment

Should this not be match.Groups[0].Value?
1

Based on the solution and awesome breakdown for matching patterns inside wrapping patterns you could try:

\[\{\#(?<Text>(?:(?!\#\}\]).)*)\#\}\]

Where \[\{\# is your escaped opening sequence of [{# and \#\}\] is the escaped closing sequence of #}].

Your inner values are in the matching group named Text.

string strRegex = @"\[\{\#(?<Text>(?:(?!\#\}\]).)*)\#\}\]";
Regex myRegex = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline);
string strTargetString = @"Employee name is [{#John#}], works for [{#ABC Bank#}], [{#Houston#}]";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    var text = myMatch.Groups["Text"].Value;

    // TODO: Do something with it.
  }
}

Comments

-2
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Test("the quick brown [{#fox#}] jumps over the lazy dog."));
            Console.ReadLine();
        }

        public static string Test(string str)
        {

            if (string.IsNullOrEmpty(str))
                return string.Empty;


            var result = System.Text.RegularExpressions.Regex.Replace(str, @".*\[{#", string.Empty, RegexOptions.Singleline);
            result = System.Text.RegularExpressions.Regex.Replace(result, @"\#}].*", string.Empty, RegexOptions.Singleline);

            return result;

        }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.