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')
Related
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!
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
I'm trying to create a regex to catch all hexadecimal colors in a string literal. I'm using Python 3, and that's what I have:
import re
pattern = re.compile(r"#[a-fA-F\d]{3}([a-fA-F\d]{3})?")
However, when I apply the findall regex method on #abcdef here's what I get:
>>> re.findall(pattern,"#abcdef")
["def"]
Can someone explain me why do I have that? I actually need to get ["#abcdef"]
Thank you in advance
According to http://regex101.com:
It looks like this regex is looking for
#(three characters a through f, A through F or a digit)(three characters a through f, A through F or a digit, which may or may not be present, and if they are they are what is returned from the match)
If you are looking to match any instance of the whole above string, I would recommend this instead:
#[a-fA-F\d]{6}
Thanks to Andrej Kesely, I got the answer to my question, that is:
Regex will return capturing group.
To bypass this, just change the regex from:
r"#[a-fA-F\d]{3}([a-fA-F\d]{3})?"
to:
r"#[a-fA-F\d]{3}(?:[a-fA-F\d]{3})?"
I need to be able to check if a string contains a number of letters and integers, and if possible how to split a string into three separate parts based off user input, any ideas?
any help is useful thanks
I think the best way to do this is to use regex. You can filter out the numbers only
import re
str1=aas30dsa20
str2=re.sub("\D", "", str1)
'3020'
You can then search for the result in the original string
start=str1.index(str2)
end=len(str1)
Finally filter it with
originalStr[start:end]
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.