• Login
Community
  • Login

Help with Regex

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
4 Posts 3 Posters 295 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P
    Pangolin
    last edited by Pangolin Mar 27, 2023, 5:10 PM Mar 27, 2023, 5:02 PM

    My knowledge with Regex is limited to add a text at the beginning of a line or the end of it.

    So I tried with ChatGpt to get a code to do what I want but without any success. I hope you will understand my explanations on what I’m looking to do.

    First EOL conversion is on LF.

    I have over text with over 30 000 lines.

    With two kinds of listing

    First :

    Movie Title
    Director
    Actor 1
    Actor 2
    Actor 3
    and so on

    Second

    Movie Title
    Actor 1
    Actor 2
    Actor 3
    and so on

    What I want to do is:
    When “Director” is present remove the first line “Movie Title” and all the lines that begins with “actor”.

    If “Director” is not present. Keep the paragraph.

    Thanks for your help

    M 1 Reply Last reply Mar 27, 2023, 11:10 PM Reply Quote 0
    • M
      Mark Olson @Pangolin
      last edited by Mark Olson Mar 27, 2023, 11:31 PM Mar 27, 2023, 11:10 PM

      @Pangolin
      Looks like this should work:
      Replace (Movie Title:.+(?:\R|\Z))(Director:.+(?:\R|\Z))?((?:Actor.*?:.+(?:\R|\Z))*) with ?2\2:\1\3
      Make sure . matches newline is turned off.

      Explanation:

      1. (?:\R|\Z) matches any newline (including, CR, CRLF, LF) or the end of the file.
      2. There are three capture groups, one for the movie title, one for director, and one for zero or more actors.
      3. In the replacement, the syntax ?2\2|\1\3 means we should replace with the second capture group (director) only if director was matched. Otherwise, we should replace with the other two capture groups (movie title and any number of actors).

      I was able to convert

      Movie Title: Baz to the Bone
      Director: Bazeven Bazlberg
      Actor 1: baz bazsson
      Actor 2: foo foosson
      Actor 3: bar barsson
      
      Movie Title: Foo Manchu
      Actor 1: foo foostein
      Actor 2: bar barstein
      Actor 3: baz bazstein
      
      Movie Title: Conan the BarBarian
      Director: Baron Dracula
      Actor 1: baz bazmeister
      Actor 2: foo foomeister
      Actor 3: bar barmeister
      

      to

      Director: Bazeven Bazlberg
      
      Movie Title: Foo Manchu
      Actor 1: foo foostein
      Actor 2: bar barstein
      Actor 3: baz bazstein
      
      Director: Baron Dracula
      
      

      Note that a ? after the second capture group is optional for this use case. The find/replace works fine either way, and performance is slightly faster if you don’t include the ?.

      However, not including the ? comes at the cost of failing to match anything where the director is not listed, which could be problematic for related use cases.

      1 Reply Last reply Reply Quote 3
      • G
        guy038
        last edited by guy038 Mar 27, 2023, 11:46 PM Mar 27, 2023, 11:42 PM

        Hello, @pangolin, @mark-olson and All,

        Here is an other formulation ! Just one constraint : add one / two line-break(s) at the very end of your file !

        SEARCH (?-si)^Movie Title.*\RDirector.*\R(?:Actor.*\R)*\R+

        REPLACE Leave EMPTY

        Notes :

        • The strings Movie Title, Director and Actor must have this exact case

        • The strings Movie Title, Director and Actor can be followed or not with a name

        • The string Actor may not be present in a paragraph block

        • The end of each block to delete may contain several consecutive line-breaks


        Test it, for instance, against this text :

        Movie Title
        Director
        Actor
        Actor
        Actor
        
        
        
        
        Movie Title
        Actor 1
        Actor 2
        Actor 3
        
        Movie Title
        Director
        
        Movie Title
        Director
        Test 1
        Test 2
        Test 3
        
        
        Movie Title
        Director ...
        Actor ...
        Actor ...
        Actor ...
        
        

        Best regards,

        guy038

        P 1 Reply Last reply Mar 28, 2023, 1:31 AM Reply Quote 0
        • P
          Pangolin @guy038
          last edited by Mar 28, 2023, 1:31 AM

          Thank you very much Mark and Guy for taking the time to answer my request. It is really appreciated!

          Have a nice evening

          1 Reply Last reply Reply Quote 0
          4 out of 4
          • First post
            4/4
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors