Menu

Strings which depends on order

Help
2003-08-07
2003-08-19
  • Nobody/Anonymous

    I request for your assistance for the following problem.

    You can a formulate the following in a regular expression.
    The RE should match, when bbbddd follows directly aaa. (in which dfsdfs.. is a substitute for any character and the rest is also simplfied)

    aaa
    dfsdfsdfsd
    bbbddd
    dfsdfsdfsd
    bbb
    dfsdfsdfsd
    bbbccc

    The RE should not match with the following example

    aaa
    dfsdfsdfsd
    bbbccc
    dfsdfsdfsd
    bbb
    dfsdfsdfsd
    bbbddd

    I tried it with assertions und condition, but i did not find a solution. i.g: aaa.*?(?=bbb)bbbddd

    Please help me.
    jr-ramses@gmx.net

     
    • Sergey A. Samokhodkin

      > The RE should match, when bbbddd follows directly aaa.

      Sounds easy: "aaabbbddd" :)

      Seem to catch it: you possibly mean "from 'aaa' to 'bbbddd' without 'bbb' in between",right?
      Not so easy... May be so:

      (?s)aaa(?:.(?!bbb(?!ddd)))*?bbbddd

      At least it works with your exapmles.

       
    • Nobody/Anonymous

      Thats it. You are great!!!
      Thank you very much.

      Only some additional questions.
      Why do i need this non capturing group?
      Is aaa.*?(?!bbb(?!ddd))bbbddd the same?

       
      • Sergey A. Samokhodkin

        > Is aaa.*?(?!bbb(?!ddd))bbbddd the same?
        Not.
        Let's take aaaXYZbbbddd as example.
        The 'aaa' part in the RE matches the 'aaa' in the target. Then the '.*?' goes: 'X' -ok, 'Y' -ok, 'Z' -not ok, quite because of the '(?!bbb(?!ddd))' !!!
        I.e. there is a an inherent contradiction within the pattern.
        Hope this helps.

        BTW, I very recommend to use the "Monitor this forum" feature in sourceforge forums. It's convenient and safe (your email wouldn't be revealed to anyone, including the owner) and doesn't reqire registration.

        Regards

        PS The question was very interesting.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.