rearding regex (python) [closed]

2024/7/6 22:41:19

I have long strings. Like this

{{ name = nameprodcuer =producerwriter = writer language = {{english}}country = USA 
}}
Here is text I Need This paragraph.;

I don't want text between {{ }}. I tried with regex but not found any luck. Can anyone help me please?

Answer

Here: http://rubular.com/r/CCW7sAUMrs is an example regex that matches whole text within {{ }}.

You can easily clean up Your string with it using re.sub.

Note, that it will not work when You have text like {{test}} paragraph {{test}}, because it will match whole string.

As larsmans said, it gets harder if You can nest braces. Then it is the job for some kind of Pushdown automaton.

Edit:

Here is the usage:

>>> text = """
... {{ name = name
...     prodcuer =producer
...     writer = writer 
...     language = {{english}}
...     country = USA 
... }}
... Here is text I Need This paragraph.;
... """
>>> import re
>>> exp = "\{\{.+\}\}"
>>> re.sub(exp, "", text, flags=re.S | re.I).strip()
'Here is text I Need This paragraph.;'
https://en.xdnf.cn/q/120702.html

Related Q&A

While Loop Guessing Number Game - Python

Im trying to make a guess the number between 1-10 game but the while loops seems to keep running. I want to program to let the user guess a number then display if its too high or low etc then start aga…

Calculating distance between word/document vectors from a nested dictionary

I have a nested dictionary as such:myDict = {a: {1:2, 2:163, 3:12, 4:67, 5:84}, about: {1:27, 2:45, 3:21, 4:10, 5:15}, apple: {1:0, 2: 5, 3:0, 4:10, 5:0}, anticipate: {1:1, 2:5, 3:0, 4:8, 5:7}, an: {1:…

Is there anything wrong with the Python code itself? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python Iterate Over String

So Ive got a string e.g "AABBCCCASSDSFGDFGHDGHRTFBFIDHFDUFGHSIFUGEGFGNODN".I want to be able to loop over 16 characters starting and print it. Then move up 1 letter, loop over 16 characters a…

what does with open do in this situation [duplicate]

This question already has answers here:What is the Python "with" statement used for?(3 answers)Closed 7 years ago.sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN D…

How to perform HTTP GET operation in Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python Error TypeError: cannot concatenate str and float objects [duplicate]

This question already has answers here:Making a string out of a string and an integer in Python [duplicate](5 answers)Closed 7 years ago.I am new with Python programming. I keep getting the below error…

Countif function in python

enter image description here In excel file i can do countif funtion like attached picture but How can i do this countif function in Python Pandas,please help me by providing the code

How do i implement these algorithms below

Alogrithm 1:Get a list of numbers L1, L2, L3....LN as argumentAssume L1 is the largest, Largest = L1Take next number Li from the list and do the followingIf Largest is less than LiLargest = LiIf Li is …

How to run a shell script once a day?

I am trying to run this particular shell script only one time, daily. Heres my code for runLucene.py:#!/usr/bin/env pythonimport os from extras.download_datos_desambiguar import news_Lucenex=datetime.t…