i want to seperate and get each one from following word
decimal(10,2)
i want:
decimal
10
2
each of these.how can we do that using regex?
",2" is optional,like in the case of char(10).The Precision value may or may not be there.
Try Regex: (\w+)(?:\((\d+)(?:,(\d+))?\))?
Demo
Simple string substitution:
import re
string = "decimal(10,2)"
string = re.sub(r'(\w+)\((\d+),(\d+)\)', r'\1\n\2\n\3', string)
print string
Related
I have following json type string text
blockAddress:{strandId:"C1DYN7Cag8oDCRRoIJ1uAz",
sequenceNo:68794},
transactionId:"AYj8Vf4kQ9EE6BJJbvt3js",
blockTimestamp:2019-12-03T08:00:04.899000001Z,
blockHash:{{gdOVqf7AsgaQf90ZK1Hsva2lzPckHnxGmm3plDRBeGA=}},
entriesHash:{{n8oUjERAqT9kL+Cr59P6UPJbIdyPvaP0R9ey9+Njdzc=}}
I want to add quotes(" ") around a word which has [a-zA-Z] characters and ends with colon(:) symbol.
Then my above string need to look likes follows:
"blockAddress":{"strandId":"C1DYN7Cag8oDCRRoIJ1uAz",
"sequenceNo":68794},
"transactionId":"AYj8Vf4kQ9EE6BJJbvt3js",
"blockTimestamp":2019-12-03T08:00:04.899000001Z,
"blockHash":{{gdOVqf7AsgaQf90ZK1Hsva2lzPckHnxGmm3plDRBeGA=}},
"entriesHash":{{n8oUjERAqT9kL+Cr59P6UPJbIdyPvaP0R9ey9+Njdzc=}}
I am trying this re.sub(r'([a-zA-Z]+:)', r'"\1"', s). But I am getting quotes after colon. Like
"blockAddress:"{"strandId:""C1DYN7Cag8oDCRRoIJ1uAz",
"sequenceNo:"68794},
"transactionId:""AYj8Vf4kQ9EE6BJJbvt3js",
"blockTimestamp:"2019-12-03T08:00:04.899000001Z,
"blockHash:"{{gdOVqf7AsgaQf90ZK1Hsva2lzPckHnxGmm3plDRBeGA=}},
"entriesHash:"{{n8oUjERAqT9kL+Cr59P6UPJbIdyPvaP0R9ey9+Njdzc=}}
What I need to change in above regex? or Is there any different approach in python?
Regex101:
txt = '''blockAddress:{strandId:"C1DYN7Cag8oDCRRoIJ1uAz",
sequenceNo:68794},
transactionId:"AYj8Vf4kQ9EE6BJJbvt3js",
blockTimestamp:2019-12-03T08:00:04.899000001Z,
blockHash:{{gdOVqf7AsgaQf90ZK1Hsva2lzPckHnxGmm3plDRBeGA=}},
entriesHash:{{n8oUjERAqT9kL+Cr59P6UPJbIdyPvaP0R9ey9+Njdzc=}}'''
import re
print( re.sub(r'([a-zA-Z]+):', r'"\1":', txt) )
Prints:
"blockAddress":{"strandId":"C1DYN7Cag8oDCRRoIJ1uAz",
"sequenceNo":68794},
"transactionId":"AYj8Vf4kQ9EE6BJJbvt3js",
"blockTimestamp":2019-12-03T08:00:04.899000001Z,
"blockHash":{{gdOVqf7AsgaQf90ZK1Hsva2lzPckHnxGmm3plDRBeGA=}},
"entriesHash":{{n8oUjERAqT9kL+Cr59P6UPJbIdyPvaP0R9ey9+Njdzc=}}
Sounds like what you want is:
re.sub(r'([a-zA-Z]+):', r'"\1":', s)
Using a lookahead assertion:
re.sub(r'([a-zA-Z]+)(?=:)', r'"\1"', input)
How can i get word example from such string:
str = "http://test-example:123/wd/hub"
I write something like that
print(str[10:str.rfind(':')])
but it doesn't work right, if string will be like
"http://tests-example:123/wd/hub"
You can use this regex to capture the value preceded by - and followed by : using lookarounds
(?<=-).+(?=:)
Regex Demo
Python code,
import re
str = "http://test-example:123/wd/hub"
print(re.search(r'(?<=-).+(?=:)', str).group())
Outputs,
example
Non-regex way to get the same is using these two splits,
str = "http://test-example:123/wd/hub"
print(str.split(':')[1].split('-')[1])
Prints,
example
You can use following non-regex because you know example is a 7 letter word:
s.split('-')[1][:7]
For any arbitrary word, that would change to:
s.split('-')[1].split(':')[0]
many ways
using splitting:
example_str = str.split('-')[-1].split(':')[0]
This is fragile, and could break if there are more hyphens or colons in the string.
using regex:
import re
pattern = re.compile(r'-(.*):')
example_str = pattern.search(str).group(1)
This still expects a particular format, but is more easily adaptable (if you know how to write regexes).
I am not sure why do you want to get a particular word from a string. I guess you wanted to see if this word is available in given string.
if that is the case, below code can be used.
import re
str1 = "http://tests-example:123/wd/hub"
matched = re.findall('example',str1)
Split on the -, and then on :
s = "http://test-example:123/wd/hub"
print(s.split('-')[1].split(':')[0])
#example
using re
import re
text = "http://test-example:123/wd/hub"
m = re.search('(?<=-).+(?=:)', text)
if m:
print(m.group())
Python strings has built-in function find:
a="http://test-example:123/wd/hub"
b="http://test-exaaaample:123/wd/hub"
print(a.find('example'))
print(b.find('example'))
will return:
12
-1
It is the index of found substring. If it equals to -1, the substring is not found in string. You can also use in keyword:
'example' in 'http://test-example:123/wd/hub'
True
I am trying to replace \ with /. However, I'm having no success.
Following is the snapshot of the scenario that I am trying to achieve
string = "//SQL-SERVER/Lacie/City of X/Linservo\171002"
print string.replace("\\","/")
Output:
//SQL-SERVER/Lacie/City of X/Linservoy002
Desired output:
//SQL-SERVER/Lacie/City of X/Linservo/171002
You need to escape "\" with an extra "\".
>>> string = "//SQL-SERVER/Lacie/City of X/Linservo\\171002"
>>> string
'//SQL-SERVER/Lacie/City of X/Linservo\\171002'
>>> print string.replace("\\","/")
//SQL-SERVER/Lacie/City of X/Linservo/171002
string = r"//SQL-SERVER/Lacie/City of X/Linservo\171002"
print string.replace("\\","/")
output
//SQL-SERVER/Lacie/City of X/Linservo/171002
You have errors both in replace function and in string definition.
In your string definition \171 gives char with octal value of 171 – y
In you replace function, backslash escapes quote.
You should escape backslashes
string = "//SQL-SERVER/Lacie/City of X/Linservo\\171002"
string.replace("\\","/")
You can simply use ".replace" in python or if you want you can use regex :
import re
string = r"//SQL-SERVER/Lacie/City of X/Linservo\171002"
pattern=r'[\\]'
replaced_string=re.sub(pattern,"/",string)
print(replaced_string)
Since your original question shows : "X/Linservo\171002" here \171 referring to character encoding so it's replacing \171 to "y". you can try this in python interpreter :
In[2]: print("\171")
y
I have a string in python, which is in this format:
[NUMBER][OPERATOR][NUMBER][UNNEEDED JUNK]
e.g.:
5+5.[)]1
How could I trim that down to just 5+5?
EDIT
I forgot to mention, basically, you just need to look for the first non-numeric character after the operator, and crop everything (starting at that point) off.
This is a simple regular expression:
import re
s = "5+5.[)]1"
s = re.search("\d+\+\d+", s).group()
print(s) # 5+5
re.search(r'\d+.\d+','123+55.[)]1').group()
This should work.
I have the following string:
text = 'adsfklaiin2007daf adf adflkajf;j 2008afadfkjkj'
I want to return:
2007 2008
Any way to do this in Python?
This is a classic case for regular expressions. Using the re python library you get:
re.findall('\d{4}', "yourStringHere")
This will return a list of all four digit items found in the string. Simply adjust your regex as needed.
import re
num = re.compile('[\d]*')
numbers = [number for number in num.findall(text) if number]
['2007', '2008']
>>> import re
>>> text = 'adsfklaiin2007daf adf adflkajf;j 2008afadfkjkj'
>>> re.sub("[^0-9]"," ",text)
' 2007 2008 '
I will leave it to you to format the output.
str.translate
text.translate(None, ''.join(chr(n) for n in range(0xFF) if chr(n) not in ' 01234567890')
You can probably construct a better table of characters to skip and make it prettier, but that's the general idea.