Write target string in console using regular expressions

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
56
Programming Experience
Beginner
Hello, everyone!

I try to write in console only one string, that contains reg text, but my code writes all text from file

Could you help me write my target string in console?
Thanks!

C#:
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.Collections.Generic[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.Linq[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.Text[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.Threading.Tasks[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.IO[/COLOR][COLOR=green];[/COLOR]
[COLOR=#0600FF]using[/COLOR] [COLOR=teal]System.Text.RegularExpressions[/COLOR][COLOR=green];[/COLOR]
 
[COLOR=#0600FF]namespace[/COLOR] ConsoleApp1
 
[COLOR=green]{[/COLOR]
    [COLOR=#6666CC]class[/COLOR] Program
 
    [COLOR=green]{[/COLOR]
        [COLOR=#0600FF]static[/COLOR] [COLOR=#6666CC]void[/COLOR] Main[COLOR=green]([/COLOR][COLOR=#6666CC]string[/COLOR][COLOR=green][[/COLOR][COLOR=green]][/COLOR] args[COLOR=green])[/COLOR]
 
        [COLOR=green]{[/COLOR]
 
 
            [COLOR=#6666CC]string[/COLOR] rootFolder [COLOR=green]=[/COLOR] [COLOR=#666666]@"H:\Test"[/COLOR][COLOR=green];[/COLOR]
 
            [COLOR=#0600FF]foreach[/COLOR] [COLOR=green]([/COLOR][COLOR=#0600FF]var[/COLOR] file [COLOR=#0600FF]in[/COLOR] Directory[COLOR=green].[/COLOR][COLOR=#0000FF]EnumerateFiles[/COLOR][COLOR=green]([/COLOR]rootFolder, [COLOR=#666666]"*"[/COLOR], SearchOption[COLOR=green].[/COLOR][COLOR=#0000FF]AllDirectories[/COLOR][COLOR=green])[/COLOR][COLOR=green])[/COLOR]
 
            [COLOR=green]{[/COLOR]
                [COLOR=teal][I]//File.AppendAllText(file, "Hi, man");[/I][/COLOR]
                StreamReader sr [COLOR=green]=[/COLOR] [COLOR=green]new[/COLOR] StreamReader[COLOR=green]([/COLOR]file, Encoding[COLOR=green].[/COLOR][COLOR=#0600FF]Default[/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
                [COLOR=#6666CC]string[/COLOR] s [COLOR=green]=[/COLOR] sr[COLOR=green].[/COLOR][COLOR=#0000FF]ReadToEnd[/COLOR][COLOR=green]([/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
                [COLOR=#6666CC]String[/COLOR] pattern [COLOR=green]=[/COLOR] [COLOR=#666666]@"\b(Vasya)\b"[/COLOR][COLOR=green];[/COLOR]
                [COLOR=#0600FF]if[/COLOR] [COLOR=green]([/COLOR]Regex[COLOR=green].[/COLOR][COLOR=#0000FF]IsMatch[/COLOR][COLOR=green]([/COLOR]s, pattern, RegexOptions[COLOR=green].[/COLOR][COLOR=#0000FF]IgnoreCase[/COLOR][COLOR=green])[/COLOR][COLOR=green])[/COLOR]
                [COLOR=green]{[/COLOR]
                    Console[COLOR=green].[/COLOR][COLOR=#0000FF]WriteLine[/COLOR][COLOR=green]([/COLOR]s[COLOR=green])[/COLOR][COLOR=green];[/COLOR]
                [COLOR=green]}[/COLOR]
 
                sr[COLOR=green].[/COLOR][COLOR=#0000FF]Close[/COLOR][COLOR=green]([/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
 
            [COLOR=green]}[/COLOR]
 
            Console[COLOR=green].[/COLOR][COLOR=#0000FF]ReadKey[/COLOR][COLOR=green]([/COLOR][COLOR=green])[/COLOR][COLOR=green];[/COLOR]
 
        [COLOR=green]}[/COLOR]
 
    [COLOR=green]}[/COLOR]
 
[COLOR=green]}[/COLOR]
 
I try to change IsMatch on Match and my codestring is uderlined

Then you did it wrong. Did you read the documentation for the Match method to see how it works? Did you search online for examples? Don't expect to simply be able to replace a call to one method with a call to the other without changing the arguments and what is done with the result. They different methods so don't assume that you can call one in exactly the same way as the other.
 
Back
Top Bottom