none
Regex formula to find "END" as whole word only RRS feed

  • Question

  • Im not the best with regex, but im trying to 

    use regex to find the word "END" as a whole word only in the following type syntax. The problem that i am having with this is that I can catch the first "END" for the first program, but it doesnt accept the second one. if i add parameters, then i end up selecting ENDIF, and ENDFOR as well

     

    &ACCESS RV

    &REL 3

    &PARAM TEMPLATE = C:\KRC\Roboter\Template\ExpertVorgabe

    &PARAM EDITMASK = *

    DEF AnalogTest( )

     

    END

     

    GLOBAL DEF ANINNoiseLog()

    DECL INT i

     

    FOR i = 1 TO 6

      IF $ANIN[i] < ANINNegMax[i] THEN

        ANINNegMax[i] = $ANIN[i]

      ENDIF

      IF $ANIN[i] > ANINPosMax[i] THEN

        ANINPosMax[i] = $ANIN[i]

      ENDIF

    ENDFOR

     

    END

     

     

    Thursday, May 12, 2011 3:35 PM

Answers

  • Hello,

    You can use boundaries like this. \bend\b


    Eyal (http://shilony.net), Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.
    Thursday, May 12, 2011 4:45 PM
    Moderator
  • This is the Regex that you need:

    Regex endMatch = new Regex("END(?=(\n|$))");

    It will look for the word END that is before a line break or at the end of the document.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Sunday, May 15, 2011 12:06 PM

All replies

  • Hello,

    You can use boundaries like this. \bend\b


    Eyal (http://shilony.net), Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.
    Thursday, May 12, 2011 4:45 PM
    Moderator
  • To get each instance of "END" you could:

     

    	  //To hold the index at which the "END" was found
          int index = 0;
          //This will get the "END" out of the string? your looking in
          Regex endGetter = new Regex("[E][N][D]");
          //This will hold the all the ENDs you get back
          List<string> endlist = new List<string>();
    
          //Gets the first match
          Match matchEND = endGetter.Match(text);
          //adds it to the list
          endlist.Add(matchEND.ToString());
          //the index of where END was found
          index = matchEND.Index;
          while (matchEND.Success)
          { 
            //We have to add 3 to it because it will give you the index of where END was found
            //So it would just keep finding the same one if we didn't
            matchEND = endGetter.Match(text,index+3);
            //the index of where the next END was found
            index = matchEND.Index;
            //If you don't have this you will have an empty string at the end of the list
            if (matchEND.Success)
            {
            endlist.Add(matchEND.ToString());
            }
          }
    
          //Just prints it out
          for (int x = 0; x < endlist.Count;x++ )
          {
            Console.WriteLine(endlist[x]);
          }

    • Proposed as answer by John McCodezy Friday, May 13, 2011 12:31 AM
    Friday, May 13, 2011 12:29 AM
  • This is the Regex that you need:

    Regex endMatch = new Regex("END(?=(\n|$))");

    It will look for the word END that is before a line break or at the end of the document.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Sunday, May 15, 2011 12:06 PM
  • I'd either go with the approach I posted or Noam's one depends on the scenario, the one posted by John McCodezy is not as efficient as it can be.
    Eyal (http://shilony.net), Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.
    Monday, May 16, 2011 7:35 AM
    Moderator