Answer the question
In order to leave comments, you need to log in
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
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.
Where can I look, what to read understandable?
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.*
Look at the methods of the String class and write your own function, what's the problem?
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.
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 questionAsk a Question
731 491 924 answers to any question