none
Looking for an unless option with regular expression RRS feed

  • Question

  • What I want to do is find every instance of "PromoHist" where "from" does not precede it on the line.

    What I'm imagining is something like ~from.*PromoHist

    Where the action is that as soon as the search engine identifies the construct preceded by ~ it moves on to the next line.

    But, perhaps I'm missing some combination of existing operators that will do the same thing.

    TIA,
    Dave

    Friday, February 8, 2013 6:25 PM

Answers

  • Try this:

    ^.*(?<!from.*)PromoHist

    It can be used programmatically or in Find dialogs of Visual Studio 2012.


    Friday, February 8, 2013 7:42 PM

All replies

  • Try this:

    ^.*(?<!from.*)PromoHist

    It can be used programmatically or in Find dialogs of Visual Studio 2012.


    Friday, February 8, 2013 7:42 PM
  • Tried that pattern first in the IDE. Then thought well perhaps the syntax for a Regex object is different. So I created a quick app with the below function and still no results.

    private void button1_Click(object sender, RoutedEventArgs e)
    {
    if (File.Exists(textBox1.Text))
    {
    try
    {
    listBox1.Items.Clear();
    StreamReader content = new StreamReader(File.Open(textBox1.Text, FileMode.Open, FileAccess.Read));
    int count = 1;
    Regex pattern = new Regex("^.*(?<!from.*)PromoHist");
    while (content.Peek() >= 0)
    {
    string line = content.ReadLine();
    if (pattern.Match(line).Success)
    {
    listBox1.Items.Add(count.ToString());
    count++;
    }
    }
    content.Close();
    listBox1.Items.Add("complete");
    }
    catch (Exception ex)
    {
    MessageBox.Show("Error: " + ex.Message);
    }
    }
    }

    I'm using 2010, and my .Net library is 4.5

    Friday, February 8, 2013 9:48 PM
  • If you want to display the line numbers, then move ‘count++’ after the ‘if’. Like this:

    using (StreamReader content = File.OpenText(textBox1.Text))

    {

                    string line;

                    int count = 1;

                    Regex pattern = new Regex("^.*(?<!from.*)PromoHist");

                    while ((line = content.ReadLine()) != null)

                    {

                                    if (pattern.IsMatch(line))

                                    {

                                                    listBox1.Items.Add(count.ToString());

                                    }

                                    ++count;

                    }

    }

    Show a sample short file were the above test does not work.

    Saturday, February 9, 2013 7:53 AM
  • The point of my reply is that the pattern found no matches. And I know it should because a plain search of PromoHist did find results that should have been included.

    A more efficient algorithm for filling a listbox is a derail.

    Monday, February 11, 2013 5:38 AM
  • Although it could be shortened to "(?<!from.*)PromoHist", I think it should work.

    Do you have an example of a line where it doesn't work?

    Monday, February 11, 2013 10:04 AM
  • I've tried the pattern using both an IDE text search, and searching them using my test program:

    lsb_SQL.Append("PH.promohist_ID,	")
    lsb_SQL.Append("JOIN promohist PH	")
    lsb_SQL.Append("FROM promohist PH, CAMP_KIT CK, CAMP_CHAIN CC, CHAIN C, CAMP CM ")
    queryBuilder.AppendLine("FROM promohist PH, CAMP_KIT CK, CAMP_CHAIN CC, CHAIN C, CAMP CM, profile_act pa, promo_reship pr, promo_request prq")
    lsb_SQL.Append(" FROM promohist")
    Private Const SQL_INSERT_promohist = "INSERT INTO promohist (CAMP_ID,	PROMO_ID,	KIT_ID,	OFFER_ID, CAMP_CONTROL_ID,PROFILE_ID, " & _
    "TDM_EXPIRATION_DT,	LAST_ACTIVITY_DT,	CREATED_DT,	CREATED_USER,	LAST_UPDATED_DT	, LAST_UPDATED_USER,	promohist_ID, " & _
    "FROM promohist P, COUPON_REDEM_PREP CRP, XREF_SF_NCH_BATCH X,  CAMP_OFFER CO, PROD_LNK PL, " & _
    "FROM promohist PH, COUPON_REDEM_PREP CRP1, " & _
    "TDM_EXPIRATION_DT,	LAST_ACTIVITY_DT,	CREATED_DT,	CREATED_USER,	LAST_UPDATED_DT	, LAST_UPDATED_USER,	promohist_ID, " & _
    "SEQ_promohist_ID.NEXTVAL, NULL, NULL, NULL, :DIVISIONID FROM COUPON_REDEM_PREP H, CAMP_CONTROL CC, PROFILE P, " & _
    "SELECT DISTINCT P.promohist_ID, " & _
    "FROM promohist PH, COUPON_REDEM_PREP CRP1, " & _
    


    Monday, February 11, 2013 3:08 PM
  • Just noticed I forgot to specify the ignore case option. The pattern works in the sample code. Unfortunately, that doesn't help most of the time. So, while I'm grateful for a pattern that works in code, I would rather it work in the IDE.

    Should be obvious that in this example what I was doing was looking for SQL statements that were not selects. I recall having other situations where I'm look for some bit of code where narrowing it would be so helpful, if the IDE interpreted a pattern the same way Regex does.

    So, the question is answered in what is for me the minority of situations.

    Sure I could create a plug-in. But, then I would need to adjust it with every new release. Don't know why but, it seems to me, MS can't help itself and alters something about it's macro/plug-in interface.

    Monday, February 11, 2013 6:38 PM
  • Hi Nottoc,

    Thank you for posting on this forum.

    >>if the IDE interpreted a pattern the same way Regex does.

    During my test, the result is the same. What happened on your side?

    >>the question is answered in what is for me the minority of situations.

    So I am marked the Viorel's suggestion as answer.

    Thank you.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 12, 2013 9:44 AM
    Moderator
  • When I search with that pattern I get no matches in the 2010 IDE.

    Tuesday, February 12, 2013 3:42 PM
  • Hi Nottoc,

    I am afraid this is because VS find dialog box is no option for  single line.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 14, 2013 9:34 AM
    Moderator