A
A
Anton20012022-03-13 17:02:18
C++ / C#
Anton2001, 2022-03-13 17:02:18

How to parse tex in such conditions?

There is an example text

куча текста , с разными знаками и символами {/.;sas slovo ,dasuwjqaas} и ешё большая куча всякого разного

How can I find slovo and parse everything inside the nearest brackets so that it turns out: {/.;sas slovo ,dasuwjqaas}
I wonder if there is a way without regular expressions, since I never learned to work with them

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
oleg_ods, 2022-03-13
@Anton2001

A string is an array of characters.
1. Using the IndexOf() method, we look for the first occurrence of the characters '{' and '}'
2. Using the SubString() method, we store the text between the found indices.
3. We return to point 1, but we start the search with the character after the '}'.
4. Repeat until one of the IndexOf() methods returns -1.
In general, to solve this problem, it is better to smoke regular seasons.

If laziness

Рефакторить и рефакторить, но работает =)
private static List<string> ParseString(string input)
        {
            List<string> result = new List<string>();

            int openIndex = input.IndexOf('{');
            int closeIndex = input.IndexOf('}');

            while (openIndex != -1 && closeIndex != -1)
            {
                result.Add(input.Substring(openIndex, closeIndex - openIndex + 1));
                
                input = input.Substring(closeIndex + 1);

                openIndex = input.IndexOf('{');
                closeIndex = input.IndexOf('}');
            }

            return result;
        }

+ вариант через регулярку Взято отсюда
private static List<string> ParseStringRegex(string input)
        {
            Regex regex = new Regex(@"{([\s\S]+?)}", RegexOptions.Compiled | RegexOptions.IgnoreCase);

            var matches = regex.Matches(input);

            return matches.Select(m => m.Value).ToList();
        }

F
freeExec, 2022-03-13
@freeExec

1. find the word
2. find the bracket before it
3. find the bracket after it
4. cut the text between them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question