2

So I'm trying to find a string within a string, now I currently have this constructed in,

string str4;

var str5 = "   type: '");
foreach (string str6 in response.Split(new char[] { '\n' }))
{
    if (str6.StartsWith(str5))
    {
        str4 = str6.Replace(str5, "").Replace(" ", "").Replace("',", "");
        break;
    }
}

Which works as expected & will grab the text from

type: '

Example of this is

type: ' EXAMPLE ',

Ouput after loop

EXAMPLE

Now the issue is that occasionally the spaces at the start of ' type: ' vary, so sometimes it may be equal to the spaces I provided, and other times it may not..

I was trying to use Regex so I could do something such as,

string str5 = "Regex(*)type: '"

Now of course that's completely incorrect in terms of usage, but my example shows the use of * which would be equal to any possibilities, so therefore no matter on the number of spaces, I would still be able to extract the innertext from type.

2
  • Variable names like str4, str5 or str6 are good for R2-D2, not for humans. Choose speaking names like result (instead of str4), searchPattern (instead of str5), line (instead of str6). Commented Jun 10, 2019 at 15:11
  • @OlivierJacot-Descombes Thanks for your input, I'll take that into account upon my next question :) Commented Jun 10, 2019 at 15:21

4 Answers 4

3

Here we would simply add optional spaces before and after our desired outputs, e.g., Example, and we can start with this expression, for instance:

type:(\s+)?'(\s+)?(.+?)(\s+)?',

Demo

or:

type:(\s+)?'(\s+)?(.+?)(\s+)?'

if we might have types of ', we would expand our expression to:

type:(\s+)?['"](\s+)?(.+?)(\s+)?['"]

Test

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"type:(\s+)?'(\s+)?(.+?)(\s+)?',";
        string input = @"type:' EXAMPLE ',
type: ' EXAMPLE ',
type:    '   EXAMPLE    ',
type:    '   Any other EXAMPLE we might have   ',";
        RegexOptions options = RegexOptions.Multiline;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Using Regex just for the sake of using Regex should be discouraged. It provides extra overhead and highly unreadable code.
@CamiloTerevinto both answers will work, I miswrote my explination, the spaces are at the start of the string, so TrimStart() & TrimEnd() will work just as well.. Thank you all for your input though! Appreciate it from all 3 of you
1

You can use .Trim(), .TrimStart() and .TrimEnd(). Using Regex looks like extra overhead which you don't really need.

7 Comments

"Now the issue is that occasionally the spaces at the start of ' type: ' vary, so sometimes it may be the equal to the spaces I provided, and other times it may not". This answer oversimplifies what's needed
@CamiloTerevinto not really. TrimStart: "Removes all leading occurrences of a set of characters specified in an array from the current String object." It doesn't matter if there are 4 spaces or 400 spaces, it will remove them all.
... and the input is type: ' EXAMPLE ' so I'm not sure why you think calling just TrimStart()/TrimEnd() will work
@CamiloTerevinto: I read that. It's a simple Trim, Replace and Trim again.
This was simply my mistake, however Mark took the time to read full description and noticed I mentioned the spaces were at the start of the string, but from my example I didn't correctly add spacing.
|
0

If this is just a simple extraction task with very limited variation in inputs, you can do it with plain Regex:

var response = @"[{
   type: 'foo',
something: 'bar',
},
{
  type: 'baz',
  something: 'bat'
}]";
var types = Regex.Matches(response, @"\s*type\:\s*\'(.+)\'")
    .Cast<Match>()
    .Select(m => m.Groups.Cast<Group>().Skip(1).Single().Value);

But it sounds like you might be trying to write a parser for a programming or markup language. If so, I would highly recommend that you don't try to do that with Regex. Regular Expressions get really hairy the moment you start trying to handle things like escaped strings ("type: 'I\'m a type: '").

If your input is in a standard format like JSON, use a parsing library for that format. If not, there are libraries like Sprache which make it easy to create powerful custom parsers.

Comments

0

First, if you are going to use Regex try out your regex strings here: https://regex101.com/

Second, If you can avoid RegEx, I would advise you to so. If a developer uses regex to solve a problem, now he has two problems. Regex can be tricky if you haven't worked with a lot. Having said that, here's another regex based solution. Also, there're usually several ways to construct a RegEx string.

using System;
using System.Text.RegularExpressions;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] SampleInputList = new string[]
            {
                "type:EXAMPLE",
                " type: EXAMPLE ",
                "   type:  EXAMPLE  "
            };

            // The following is a non-capture group indicator: (?:)
            // non-capture groups are a good way to organize parts
            // of your regex string and can help you visualize the
            // parts that are just markers like 'type:' vs. the parts
            // that you want to actually manipulate in the code.
            Regex expression = new Regex(@"(?:\s*type\:\s*)([A-Za-z]*)");

            foreach (string Sample in SampleInputList)
            {
                MatchCollection matches = expression.Matches(Sample);
                if (matches.Count > 0)
                {
                    GroupCollection groups = matches[0].Groups;
                    if (groups.Count > 1)
                    {
                        Console.WriteLine(groups[1].Value);
                    }
                }
            }
        }
    }
}

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.