Python Forum
Regular Expression search to comment lines of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular Expression search to comment lines of code
#6
(Sep-06-2022, 09:36 PM)Gman2233 Wrote: Then, I want to read this file and add line comments.. '--' to the beginning if each line in the original DDL script (A different file) that adds the erroneous constraints:
ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE)
~becomes~
-- ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE)

Why write a script to do this at all? If you're using this to learn Python, that's fine, but do be aware that there are often other tools that can help. In this case, at least on Unix*, I'd use sed. Assuming the file is called changes.sql:

Output:
$ cat changes.sql ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE) $ sed -i .original 's/^/--/' changes.sql $ cat changes.sql --ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE) $ cat changes.sql.original ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE)
So, to break down what the sed command does here:

- The -i option will modify the file in place, but leave the original in a file whose name has the given extension (.original here).
- 's/^/--/' specifies what we want sed to do - substitute the beginning of the line (i.e. the regular expression ^) with the comment characters --. It will do this for each line.

Printing out the file shows that the line has been changed and printing out the one with .original on the end shows we've indeed kept the original there.

A good tutorial on sed can be found here: https://www.grymoire.com/Unix/Sed.html.

Note also that text editors are powerful these days and have useful search and replace functionality, so you may choose to do it that way.

* I don't use Windows, so I don't know what the options are there.
Reply


Messages In This Thread
RE: Regular Expression search to comment lines of code - by ndc85430 - Sep-08-2022, 06:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 220 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  data validation with specific regular expression shaheen07 0 346 Jan-12-2024, 07:56 AM
Last Post: shaheen07
Question remove all comment ? SpongeB0B 7 1,378 Oct-27-2023, 05:40 PM
Last Post: deanhystad
  Is the following code returning a generator expression? quazirfan 8 1,645 Apr-11-2023, 11:44 PM
Last Post: quazirfan
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,680 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Regex Expression With Code Query In Pandas eddywinch82 8 2,363 Apr-13-2022, 09:12 AM
Last Post: snippsat
  Need help with my code (regular expression) shailc 5 1,944 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,187 Mar-09-2022, 10:34 PM
Last Post: snippsat
  I want to simplify this python code into fewer lines, it's about string mandaxyz 5 2,137 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
  regular expression question Skaperen 4 2,516 Aug-23-2021, 06:01 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020