A
A
Ai Lab2016-01-23 08:44:29
Programming
Ai Lab, 2016-01-23 08:44:29

How to correctly search for matches with reculars of the form "*tutext*tuteschetext*", where * is any sequence of characters?

Unfortunately, I didn’t comprehend the zen of regular expressions and I don’t understand how they are built, I tried to find a match with similar strings in different ways, but it’s useless, maybe someone tried to do this or where can I look, what to read understandable?
Example:
Plaintext : syvolrayllovra yolva TEXT1 swivelfoyrw TEXT2 syllabary regex
: *TEXT1*TEXT2*
Result : match

Answer the question

In order to leave comments, you need to log in

6 answer(s)
I
Ivanq, 2016-01-23
@Ivanq

Instead of *, write .* An asterisk indicates from 0 repetitions of the previous character. Plus means from 1 repetition. A dot is any character. .* - any number of any characters.

A
abs0lut, 2016-01-23
@abs0lut

Where can I look, what to read understandable?

Mastering Regular Expressions by Jeffrey EF Friedl

Y
Yuri, 2016-01-23
@riky

and why in the second case there should be a coincidence?
if TEXT1 you want to optionally do so, write the
regular expression:
.*(TEXT1)?.*TEXT2.*

D
Daniil Demidko, 2016-01-23
@Daniro_San

Look at the methods of the String class and write your own function, what's the problem?

D
Dmitry Kovalsky, 2016-01-23
@dmitryKovalskiy

Alternatively, you can call IndexOf twice on the source text, passing first text1, then text2. If -1 is returned, then these phrases do not exist. if (indexOf(text1) + text1.Length) are equal to IndexOf(text2) - then the phrases are consecutive. Well, this is probably not the most accurate description, but I think the idea is clear.

K
kwayne, 2016-01-30
@kwayne

If you need to find any text in the TEXT1 format, i.e. - a sequence of capital letters with numbers at the end, then:

string s = " ыволраыловра ыва ыолва ТЕКСТ1 фырвлфоырв ТЕКСТ2 фыоврфлыв ";
            Regex reg = new Regex  (@"[А-Я]+\d+");
            MatchCollection m = reg.Matches(s);
            Console.Write("Строка:" + s);
            foreach (Match ma in m)
            {
                Console.Write("\n" + ma); 
            }
            Console.ReadKey();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question