Gideon Isaac
Gideon Isaac

Reputation: 405

get a line with regex

I'm having trouble doing simple things with regex in dot net. Suppose I want to find all lines that contain the word "pizza". I would think I would do the following:

^ .* pizza .* $

The idea is the first character indicates the start of a line, the dollar sign indicates the end of the line, and the dot-star indicates any number of characters. This doesn't seem to work.

Then I tried something else that doesn't work either. I thought I would find all routines in my visual basic project that start with "Sub Page_Load" and end with "End Sub". I did a search for:

 Sub Page_Load .* End Sub

But this found pretty much EVERY subroutine in the project. In other words, it didn't limit itself to the Page_Load sub. So I thought I'd be smart and notice that every End Sub is at the end of a line, so all I have to do is put a $ after it like this:

 Sub Page_Load .* End Sub$

But that finds absolutely zero strings. So what am I doing wrong? (one note, I put extra blanks around .* here so you can see it, but normally the blanks would not be there.

Upvotes: 0

Views: 335

Answers (2)

stema
stema

Reputation: 93046

So, now complete new answer.

  1. Search for the word "pizza" (not "pizzas")

    1. If you have a Multiline string and want to find a single row, you need to use the Option [Multiline][1]. That changes the behaviour of the anchors ^ and $ to match the start and the end of the row.

    2. To ensure to match only the complete word "pizza" and no partial match, use word boundaries

    3. If you don't use the Singleline option, you don't need to worry about greediness

    So your regex would be:

    Regex optionRegex = new Regex(@"^.*\bpizza\b.*$", RegexOptions.Multiline);
    
  2. For the Sub Page_Load.*End Sub thing, you need to match more than one line:

    1. Use the single line option, to allow the . match also newline characters.

    2. You need ungreedy matching behaviour of the quantifier

    So your regex would be:

    Regex optionRegex = new Regex(@"Sub Page_Load.*?End Sub", RegexOptions.Singleline);
    

Upvotes: 0

Raheel Hasan
Raheel Hasan

Reputation: 6031

you may need non-greedy approach. try this:

^.*?pizza.*$

Upvotes: 1

Related Questions