问题
I have ugly string:
oldstr = "0.100% fDrg: 2%,dgdv: 29% fGd dg 0.2%, Ghh-sf 2.2 dbgh: NONE dfgdf6 gd 3 "
I need to insert char |
after the last digit of number for next splitting by this inserted |
. There is also value none
, where is also added this separator:
0.100| fdrg: 2|,dgdv: 29| fgd dg 0.2|, ghh-sf 2.2|dbgh: none| dfgdf6|gd 3|
I try this, but no success:
print re.sub(r'(\d+[a-z %^.])', r'\1|', oldstr.lower())
0.|100%| fdrg: 2%|,dgdv: 29%| fgd dg 0.|2%|, ghh-sf 2.|2 |dbgh: none dfgdf6 |gd 3 |
Any help will be appreciated.
回答1:
You can use
(\bnone\b|\d+(?:\.\d+)?)%?
And replace with \1|
.
Explanation:
(\bnone\b|\d+(?:\.\d+)?)
- Group 1 matching 2 alternatives:\bnone\b
- whole wordnone
|
- or...\d+(?:\.\d+)?
- a float value (\d+
matches one or more digits, and(?:\.\d+)?
matches (optionally) a dot followed with one or more digits)
%?
- an optional (since?
means match one or zero times)%
symbol
See regex demo
Python code:
import re
p = re.compile(ur'(\bnone\b|\d+(?:\.\d+)?)%?', re.IGNORECASE)
test_str = "0.100% fDrg: 2%,dgdv: 29% fGd dg 0.2%, Ghh-sf 2.2 dbgh: NONE dfgdf6 gd 3 "
subst = "\1|"
result = re.sub(p, subst, test_str)
If you need to trim the values, you will be able to do it after splitting. Also, none
can be turned lower case before processing the text with re.sub(r'\b\none\b', 'NONE', input)
.
回答2:
How about:
>>> re.sub(r"([\d\.]+|NONE)%?", r"\1|", oldstr)
'0.100| fDrg: 2|,dgdv: 29| fGd dg 0.2|, Ghh-sf 2.2| dbgh: NONE| dfgdf6| gd 3| '
Here we are capturing one or more occurences of digits and dots or a NONE
in a capturing group (followed by an optional %
sign) and replacing it with itself and a pipe character.
Note that @Wiktor's capturing part of the regular expression is much better than in this answer.
回答3:
import re
oldstr = "0.100% fDrg: 2%,dgdv: 29% fGd dg 0.2%, Ghh-sf 2.2 dbgh: NONE dfgdf6 gd 3"
newstring = re.sub(r"([\.\d]+)", r"\1|", oldstr)
print newstring.replace("%","").replace("NONE","NONE|")
output:
0.100| fDrg: 2|,dgdv: 29| fGd dg 0.2|, Ghh-sf 2.2| dbgh: NONE| dfgdf6| gd 3|
After a little more thinking here is a one-liner:
print re.sub(r"([\.\d'NONE']+)%?", r"\1|", oldstr)
来源:https://stackoverflow.com/questions/34857379/insert-char-to-string-to-end-of-number