正则表达式,“除字符串结尾之外”,问题。 [英] Regular expression, "except end of string", question.
问题描述
您好,
我的字符串如下:
string =" WHITE / CLARET / PINK / XL"
我需要更改为这种格式:
string =" WHITE-CLARET-PINK / XL"
我开发了以下功能。但是,有些东西是错的
我的正则表达式。它目前符合以下条件:
/ CLARET
/ PINK
/ XL
我想也许我可以用这样的东西排除包含
字符串结尾的匹配:
r"([/] \\ \\ w + [^ $])"
但这也没有用。谁能告诉我如何排除最后的
比赛?字符串结尾的那个/ XL。
万分感谢,
Derek Basch
------------------------------------------------
导入重新
string =" WHITE / CLARET / PINK / XL"
def replace_fs(thematch ):
thematch = thematch.group(1)
thematch =" - " + thematch [1:]
返回匹配
bs_regex = re.compile(r"([/] \ w +)")。sub( replace_fs,str(string))
print bs_regex
)"
但这也没有用。谁能告诉我如何排除最后的
比赛?字符串结尾的那个/ XL。
万分感谢,
Derek Basch
------------------------------------------------
导入重新
string =" WHITE / CLARET / PINK / XL"
def replace_fs(thematch ):
thematch = thematch.group(1)
thematch =" - " + thematch [1:]
返回匹配
bs_regex = re.compile(r"([/] \ w +)")。sub( replace_fs,str(string))
print bs_regex
在文章< MP ************* ***********@news-server.san.rr.com> ;,
Derek Basch< db **** @ yahoo.com>写道:
string =" WHITE / CLARET / PINK / XL"
我需要改为这种格式:
> string =" WHITE-CLARET-PINK / XL"
你真的需要regexps吗?
< blockquote class =post_quotes>string =" WHITE / CLARET / PINK / XL"
'' - ''。join(string.split(''/'',2 ))
''WHITE-CLARET-PINK / XL''
-
David Eppstein http://www.ics.uci.edu / ~eppstein /
大学加州,欧文,信息学院和计算机科学
Derek Basch写道:
我有一个字符串:
string =" ; WHITE / CLARET / PINK / XL"
我需要改为这种格式:
string =" WHITE-CLARET-PINK / XL"
我开发了以下功能来实现这一目标。但是,我的正则表达式出了问题。
正如他们所说,如果你有问题并且想一个正则表达式
是解决它的最好方法,你现在可能有两个问题...
你总是想要排除最后一个/ ?怎么样呢
代替:
s =''white / claret / pink / xl''
s2 = s.split(''/'')#临时列表
#加入所有但连续使用连字符,斜杠后添加最后
s ='' - ''.join(s2 [: - 1])+''/''+ s2 [-1]
#s现在是''white-claret-pink / xl' '
取决于格式是什么(例如/ XL
实际上是可选的吗?)它可能比
$更简单或更难b $ b这个。
可能也可以使用,但也许它可能不会很清楚。不会很清楚。 >
-Peter
Hello,
I have a string like:
string = "WHITE/CLARET/PINK/XL"
which I need to alter to this format:
string = "WHITE-CLARET-PINK/XL"
I developed the below functions to do so. However, something is wrong
with my regular expression. It currently matches with the following:
/CLARET
/PINK
/XL
I thought perhaps I could exclude the match that includes the end of the
string with someting like this:
r"([/]\w+[^$])"
but that didnt work either. Can anyone tell me how to exclude the last
match? The one with the end of the string "/XL".
Thanks a million,
Derek Basch
------------------------------------------------
import re
string = "WHITE/CLARET/PINK/XL"
def replace_fs(thematch):
thematch = thematch.group(1)
thematch = "-" + thematch[1:]
return thematch
bs_regex = re.compile(r"([/]\w+)").sub(replace_fs, str(string))
print bs_regex
)"
but that didnt work either. Can anyone tell me how to exclude the last
match? The one with the end of the string "/XL".
Thanks a million,
Derek Basch
------------------------------------------------
import re
string = "WHITE/CLARET/PINK/XL"
def replace_fs(thematch):
thematch = thematch.group(1)
thematch = "-" + thematch[1:]
return thematch
bs_regex = re.compile(r"([/]\w+)").sub(replace_fs, str(string))
print bs_regex
In article <MP************************@news-server.san.rr.com>,
Derek Basch <db****@yahoo.com> wrote:
string = "WHITE/CLARET/PINK/XL"
which I need to alter to this format:
string = "WHITE-CLARET-PINK/XL"
Do you really need regexps for this?
string = "WHITE/CLARET/PINK/XL"
''-''.join(string.split(''/'',2))
''WHITE-CLARET-PINK/XL''
--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Derek Basch wrote:
I have a string like:
string = "WHITE/CLARET/PINK/XL"
which I need to alter to this format:
string = "WHITE-CLARET-PINK/XL"
I developed the below functions to do so. However, something is wrong
with my regular expression.
As they say, if you have a problem and think a regular expression
is the best way to solve it, you might now have two problems...
Do you always want to exclude the last "/" ? How about this
instead:
s = ''white/claret/pink/xl''
s2 = s.split(''/'') # temporary list
# join all but last with hyphens, add last after slash
s = ''-''.join(s2[:-1]) + ''/'' + s2[-1]
# s is now ''white-claret-pink/xl''
Depending on what the format really is (e.g. is the /XL
actually optional?) it might be simpler or harder than
this.
An re can be made to work too, probably, but perhaps it
won''t be very clear.
-Peter
这篇关于正则表达式,“除字符串结尾之外”,问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!