Get the string within brackets and remove useless string in Python - python

I have a string like this '0x69313430303239377678(i1400297vx)' I only want the value i1400297vx and nothing else.
Is there a simple way for example using strip method or I'm forced to use Regex,I'm not good at...
Someone could kindly help me?

This works, using split and strip:
'0x69313430303239377678(i1400297vx)'.split('(')[1].strip(')')
but a regex would be more readable!

Related

Problem with Regex "The string is outside the <li></li>" Python

I need to get string which are not attachments.
(Is not between <li></li>)
Example:
i need this string!
<div>i need this string too!</div>
:)<li>I don't need this string!</li>:)
Result:
i need this string!
<div>i need this string too!</div>
:):)
I tried this:
^(?!<li>.*$).*(?<!<\/li>)
But the problem is that if there is text next to my string
<li>text</li>
For example a smiley:
:)<li>I don't need this string!</li>:)
then the pattern does not work and I don’t understand how to fix it.
Can you correct me please?
Use re.sub() to remove the string you don't want, instead of trying to match everything else.
result = re.sub('<li>.*?</li>', '', text);

Replace string with quotes, brackets, braces, and slashes in python

I have a string where I am trying to replace ["{\" with [{" and all \" with ".
I am struggling to find the right syntax in order to do this, does anyone have a solid understanding of how to do this?
I am working with JSON, and I am inserting a string into the JSON properties. This caused it to put a single quotes around my inserted data from my variable, and I need those single quotes gone. I tried to do json.dumps() on the data and do a string replace, but it does not work.
Any help is appreciated. Thank you.
You can use the replace method.
See documentation and examples here
I would recommend maybe posting more of your code below so we can suggest a better answer. Just based on the information you have provided, I would say that what you are looking for are escape characters. I may be able to help more once you provide us with more info!
Use the target/replacement strings as arguments to replace().
The general format is mystring = mystring.replace("old_text", "new_text")
Since your target strings have backslashes, you also probably want to use raw strings to prevent them from being interpreted as special characters.
mystring = "something"
mystring = mystring.replace(r'["{\"', '[{"')
mystring = mystring.replace(r'\"', '"')
if its two characters you want to replace then you have to first check for first character and then the second(which should be present just after the first one and so on) and shift(shorten the whole array by 3 elements in first case whenever the condition is satisfied and in the second case delete \ from the array.
You can also find particular substring by using inbuilt function and replace it by using replace() function to insert the string you want in its place

Splitting a string by integers in Python

Lets say i have a string 'hvusdhf4673bfiw67bfe'.
I want to split it in 'hvusdhf' and 'bfiw' and 'bfe' using str.split().I have not been able to do that. Can somebody help here?
Use regex
import re
re.split('\d+', 'hvusdhf4673bfiw67bfe')

Split lines by a character or whitespace python

I'm trying to split the lines in the data file I'm playing with. This was originally someone else's code, just trying to 'fix it'. They have it splitting on a semi-colon, but I realized that they actually need it to split on excess whitespace as well. I've singled my problem out to the expression in line 28. I was trying some suggestions from other users, but when I use a regex command I get an invalid literal for int() warning. This is confusing because it works if I don't use the regex. Any suggestions? Thanks.
EDIT: Edited for full code link.
No, .split with no arguments is the only form that splits on any whitespace.
Use a regex like this:
re.split(r'[\s;]+', text)

How to find if a string contains all the certain characters?

For example:
Characters to match: 'czk'
string1: 'zack' Matches
string2: 'zak' Does not match
I tried (c)+(k)+(z) and [ckz] which are obviously wrong. I feel this is a simple task, but i am unable to find an answer
The most natural way would probably to use sets rather than regex, like so
set('czk').issubset(s)
Code is very often simpler and easier to maintain without using regex much.
Basically you have to sort the string first so you get "ackz" and then you can use a regex like /.*c.*k.*z.*/ to match against.

Categories

Resources