ignoring newline character in regex match

2024/4/15 1:58:36

I am trying to replace all matching occurrences with title cases using the following script. When there is a newline character between filter words (in this case 'ABC' and 'DEF') that line doesn't get replaced as intended.

How can I ignore the newline character in this case?

Edit: I don't want to strip all newline characters entirely from the string, but only strip those between the filter words.

Edit2: I edited the text and script to better reflect on the issue I am experiencing. If I include flags=re.DOTALL argument, it will give me:

  mmm    = "Hello Hello Hello Hello Hello HelloHello Hello Hello Hello",Bbb   = "Bbb",

whereas the output I want is (notice that bbb is not capitalized):

  mmm    = "Hello Hello Hello Hello Hello HelloHello Hello Hello Hello",bbb   = "bbb",

The following is the script I am using.

test_string = '''mmm    = "hello hello hello hello hello hellohello hello hello hello",bbb   = "bbb",
'''rex = r'(?<= mmm)(.*)(?=\")'def maketitle(match_obj):return match_obj.group(0).title()formatted = re.sub(rex, maketitle, test_string, flags=re.DOTALL)print(formatted)

Use the re.DOTALL flag:

formatted = re.sub(rex, maketitle, string, flags=re.DOTALL)

According to the docs:

Make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.


Related Q&A

Xml parsing from web response

Im trying to get response from nominatim to geo-code few thousands of cities. import os import requests import xml.etree.ElementTree as ETtxt = open(input.txt, r).readlines() for line in txt:lp, region…

How to Install rpy2 on Mac OS X

I am trying, so far unsuccessfully, at installing the rpy2 for python on my Mac OSX. I have tried Macports and DarwinPorts but have had no luck with import rpy2 within the python shell environment. I…

Socket.IO vs. Twisted [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

How do I make Django ManyToMany through queries more efficient?

Im using a ManyToManyField with a through class and this results in a lot of queries when fetching a list of things. Im wondering if theres a more efficient way.For example here are some simplified cla…

How do I structure my Python project to allow named modules to be imported from sub directories

This is my directory structure:Projects+ Project_1+ Project_2- Project_3- Lib1__init__.py # emptymoduleA.py- Tests__init__.py # emptyfoo_tests.pybar_tests.pysetpath.py__init__.py # emptyfoo.pybar.p…

Plotly - Adding Scatter Geo points and traces on top of Density Mapbox

I am trying to add a Scattergeo trace or overlay on top of a white-bg density mapbox to get a heat map over a generic USA states outline. The reason for my use of scattergeo is Id like to plot a star s…

Removing items randomly from a dictionary

How do I remove random items from a dictionary in Python?I have to remove a specified number of items from a dictionary and so I tried to use dict.popitem which I thought was random, but it is seems i…

Requests-html results in OSError: [Errno 8] Exec format error when calling html.render()

I am using requests-html and trying the render function, with little success. When I run this script using python3.8 #!/usr/bin/python3 from requests_html import HTML file = "scrape/temp_file2.ht…

Python setuptools: packaging the root directory (no subdirectory per package wanted)

I need to write a package into a repository, but it is a small quick package, so I dont see the need to put files into a subdirectory. I simply want: import mypkg.module1with directory structure root_f…

ImportError when trying to import python module in SublimeText2

Im new to SublimeText2. So far I have found it excellent, but I just came across a problem I did not manage to solve. Im trying to import a Python module, mechanize, into my script. However, whenever a…