Click here to Skip to main content
15,896,549 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What's the regular expression to check if a string starts with for example ==> "RE" "4T" AND "4S"
The idea is I want to delete any line which starts with the above patter
but should any line should contain say "RE" IN-BETWEEN IT SHOULD NOT DELETE .
I just want to delete it when it begins with these=>"RE" "4T" AND "4S"
These what I have so far but it deletes lines that contains the above strings
Like if a line is CODEpROJECT_RESTING-WH It would format everything from RESTING-WH .But I would like to delete the whole line if it starts with "RE"


C#
string rchTxtContent.Text;

     //This Line of code do formats for ==>"RE" within the RichTextBox at_all_times.
     var cLearOut_RE_WithinRichTextBox = new Regex(@"RE(.*)", RegexOptions.Multiline);
     rchTxtContent.Text = cLearOut_RE_WithinRichTextBox.Replace(rchTxtContent.Text, string.Empty);
     rchTxtContent.Lines = (from code_Projects in rchTxtContent.Lines
                            let x = code_Projects.IndexOf("RE")
                            select (x >= 0 ? code_Projects.Substring(0, x) : code_Projects)).ToArray();


Thanks for any advice.
Posted
Updated 4-Jun-15 5:09am
v2
Comments
PIEBALDconsult 4-Jun-15 11:19am    
^RE(.*)
Julia Fraveau 4-Jun-15 11:27am    
Ok PIEBALDconsult right away sir.
Julia Fraveau 4-Jun-15 11:29am    
Just tried your solution,but is the same like mine @PIEBALDConsult.

Try:
^(RE|4T|4S)
 
Share this answer
 
Comments
Julia Fraveau 4-Jun-15 11:26am    
Thanks @OriginalGiff, you mean in the place of the IndexOf?
OriginalGriff 4-Jun-15 11:50am    
Your questions was:
"What's the regular expression to check if ..."
So I'd start by looking at using it in the regex myself...:laugh:
Julia Fraveau 4-Jun-15 11:56am    
You are the man @OginalGiff..
Andreas Gieriet 4-Jun-15 15:59pm    
My 5!
Cheers
Andi
PS: You might use a non-capturing group (?:...) instead of the capturing group (...).
following on from Griff's solution:
C#
Regex matcher = new Regex("^(RE|4T|4S)", RegexOptions.ExplicitCapture | RegexOptions.Compiled);
rchTxtContent.Lines = (from code_Projects in rchTxtContent.Lines
                       where !matcher.IsMatch(code_Projects)
                       select code_Projects).ToArray();

This will just remove the lines that begin with any of the 3 two-character strings.

Additional considerations:
If this is a compile-time fixed pattern, then make the matcher a private static readonly field of the class (instead of a local variable of the method) and then the construction and compilation (relatively expensive) of the Regex will happen only once.
You can add the RegexOptions.IgnoreCase to the constructor if you need this to be case insensitive.
 
Share this answer
 
Comments
Andreas Gieriet 4-Jun-15 15:59pm    
My 5!
Cheers
Andi
PS: Same here as for OriginalGriff: You might use a non-capturing group (?:...) instead of the capturing group (...).
Matt T Heffron 4-Jun-15 16:36pm    
But the RegexOptions.ExplicitCapture means that the unnamed group does not actually capture.
Andreas Gieriet 5-Jun-15 3:21am    
You are right - I missed that option in the code...
I'm so used to have both kind of groups in regular expressions that I never use that option.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900