Removing characters from the beginning and end or only to the end of a line

I want to remove some characters from a string using a regular expression, for example:

== (which are found both at the beginning and at the end of the line),

* (only at the beginning of the line).

def some_func():
    clean = re.sub(r'= {2,}', '', clean) #Removes 2 or more occurrences of = at the beg and at the end of a line.
    clean = re.sub(r'^\* {1,}', '', clean) #Removes 1 or more occurrences of * at the beginning of a line.

What happened to my code? It seems that the expressions are wrong. How to delete a character / character if it is at the beginning or at the end of a line (with one or more occurrences)?

+3
source share
5 answers

If you want to remove characters only from the beginning and end, you can use the method string.strip(). This will give the following code:

>>> s1 = '== foo bar =='
>>> s1.strip('=')
' foo bar '
>>> s2 = '* foo bar'
>>> s2.lstrip('*')
' foo bar'

strip , , ltrip , rstrip .

, :

clean = re.sub(r'(^={2,})|(={2,}$)', '', clean)
clean = re.sub(r'^\*+', '', clean)

IMHO, strip/lstrip/rstrip, , .

: , , :

clean = clean.lstrip('*').strip('= ')

( , , , - , , .strip('= ') "=" "" , "=".)

+6

. .

r'^(?:\*|==)|==$'
+3

, "{"... , .

"=" ( ) , ...

clean = re.sub(r'^(==+)?(.*?)(==+)?$', r'\2', s)

"^", "$", ( ).

0

, ?

tu = ('======constellation==' , '==constant=====' ,
      '=flower===' , '===bingo=' ,
      '***seashore***' , '*winter*' ,
      '====***conditions=**' , '=***trees====***' , 
      '***=information***=' , '*=informative***==' )

import re
RE = '((===*)|\**)?(([^=]|=(?!=+\Z))+)'
pat = re.compile(RE)

for ch in tu:
    print ch,'  ',pat.match(ch).group(3)

:

======constellation==    constellation
==constant=====    constant
=flower===    =flower
===bingo=    bingo=
***seashore***    seashore***
*winter*    winter*
====***conditions=**    ***conditions=**
=***trees====***    =***trees====***
***=information***=    =information***=
*=informative***==    =informative***

==== *** = **, = **?

*** ==== ==== ***, ==== ***?

? **

0

, :

tu = ('======constellation==' , '==constant=====' ,
      '=flower===' , '===bingo=' ,
      '***seashore***' , '*winter*' ,
      '====***conditions=**' , '=***trees====***' , 
      '***=information***=' , '*=informative***==' )

import re,codecs

with codecs.open('testu.txt', encoding='utf-8', mode='w') as f:
    pat = re.compile('(?:==+|\*+)?(.*?)(?:==+)?\Z')
    xam = max(map(len,tu)) + 3
    res = '\n'.join(ch.ljust(xam) + pat.match(ch).group(1)
                    for ch in tu)
    f.write(res)
    print res

Where is my brain when I wrote RE in my earlier post ?! OO! Not a greedy quantifier. *? to == + \ Z is a real solution.

0
source

Source: https://habr.com/ru/post/1773392/


All Articles