how to replace specific characters with different rules with python? [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file and some substitution is needed: replace "," with "," and all other characters not in rule2 with a whitespace, how can I do that?

What about this?
text = text.replace(",", ",")

You can use the regular expressions module:
text = re.sub(',', ',', text)
text = re.sub(negated_rule2, ' ', text)
where your negation of "rule2" is formatted using the regular expressions syntax (see link above).

Related

negative lookbehind not working as expected [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 reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have strings of this form:
FPLBX(2x3)ZE(53x13)(4x7)ZGQO
I want to find the blocks in parenthesis but only when they're not preceded by another group.
The other way around works perfectly fine but I can't make it work with preceding.
current regex:
(\(\d*x\d*\))(?<!\))
You simply need to put the so-called negative lookbehind assertion, i.e. the (?<!\))-part, in front of your search re:
>>> import re
>>> txt = "FPLBX(2x3)ZE(53x13)(4x7)ZGQO"
>>> re.findall(r"(?<!\))(\(\d*x\d*\))", txt)
['(2x3)', '(53x13)']

How to replace a ' from a String in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to remove a " ' " character from a python string .
Below code gives a syntax error, How to achieve this task
final = string.replace(old_str, '\'', '')
To replace single quotes
final = old_str.replace("'", "")
str = "this is string example....wow!!! this is really string"
print(str.replace("is", "was"))
Here's an example of how to correctly use the replace function. This replaces all the is's with was's. It's hard to see what you're trying to do but use this as a guide.
You can do this
string = "some ' string"
final = string.replace('\'','')
print(final)
output
some string

Reverse and split string: Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to reverse and split a string in Python. Please suggest how?
'this is Xing Min' should return ['niM', 'gniX', 'si', 'siht'].
You can do it as:
my_str[::-1].split()
Example
>>> s = 'Hello World'
>>> print s[::-1].split()
['dlroW'. 'olleH']
>>> s = 'this is Xing Min'
>>> print s[::-1].split()
['niM', 'gniX', 'si', 'siht']
Here, the [::-1] gets the whole string in reverse order. This is the syntax [start:end:step]. When you don't specify a start and end, it will deal with the whole string. When you do [::-1], the step value is -1 which means that the string is read in reverse.

python parsing string using regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to parse a string from this:
CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt
To this:
ES1-HER / ES1-Seura
Any easy way to do this with regex?
>>> import re
>>> s = 'CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt'
>>> re.findall('OU=([^,]+)', s)
['Computers', 'ES1-HER', 'ES1-Seura', 'RES-ES1']
>>> re.findall('OU=([^,]+)', s)[1:3]
['ES1-HER', 'ES1-Seura']
>>> ' / '.join(re.findall('OU=([^,]+)', s)[1:3])
'ES1-HER / ES1-Seura'
Don't use str as a variable name. It shadows builtin function str.

Proper regex for this URL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What regex would I use to get urls that follow this pattern:
'https://www.facebook.com/' + 'some text' + 'browser'
Meaning that it starts with the facebook url, has some text which varies, and then has the word 'browser' at the end?
Thanks!
r'https://www\.facebook\.com/.*browser'
. is a regex metacharacter meaning "any single character", so the literal periods have to be escaped with backslashes. * means "any number of matches for the previous thing", so .* means "any number of arbitrary characters". The r in front of the string marks it as a raw string literal, so backslashes are processed by the regex engine instead of the Python parser.
I think it is r'https://www\.facebook\.com/.*browser$' , because the word 'browser' is at the end.

Categories

Resources