在字符串中的所有数字前面插入“ - ”字符 [英] Inserting '-' character in front of all numbers in a string

查看:517
本文介绍了在字符串中的所有数字前面插入“ - ”字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,


我希望能够在字符串中的所有数字

值前插入一个'' - ''字符。我想在

中使用getopt.getopt()函数插入'' - ''字符。


Rigt now,I''实现菜单系统,用户将能够选择一组选项,例如2a 3ab。对应菜单

选择。但是,使用getopt.getopt(),如果输入-2a -3ab作为我的字符串,它将只返回我想要的
。我不希望用户必须在他们所有的选择前插入

a'' - ''字符,所以我在考虑

首先接受字符串输入,然后添加'' - ''字符

我自己。


所以我的问题是,如何更改:

" 2a 3ab"进入-2a -3ab。


正则表达式? :/

解决方案

3月30日上午10点38分,kevinliu23 < kevinli ... @ gmail.comwrote:


嘿伙计们,


我希望能插入一个' - ''字符在字符串中的所有数字

值前面。我想在

中使用getopt.getopt()函数插入'' - ''字符。


Rigt now,I''实现菜单系统,用户将能够选择一组选项,例如2a 3ab。对应菜单

选择。但是,使用getopt.getopt(),如果输入-2a -3ab作为我的字符串,它将只返回我想要的
。我不希望用户必须在他们所有的选择前插入

a'' - ''字符,所以我在考虑

首先接受字符串输入,然后添加'' - ''字符

我自己。


所以我的问题是,如何更改:

" 2a 3ab"进入-2a -3ab。


正则表达式? :/



正则表达式肯定会有效。这里有一个黑客:


tempInput =''2a 3ab''

tempLst = tempInput.split('''')


输出=''''
我在tempLst中的


输出+ =('' - ''+ i +'''' ')

我确定有比这更好,更优雅的黑客。


Mike


kevinliu23写道:


嘿伙计们,


我希望能插入一个' - ''字符在字符串中的所有数字

值前面。我想在

中使用getopt.getopt()函数插入'' - ''字符。


Rigt now,I''实现菜单系统,用户将能够选择一组选项,例如2a 3ab。对应菜单

选择。但是,使用getopt.getopt(),如果输入-2a -3ab作为我的字符串,它将只返回我想要的
。我不希望用户必须在他们所有的选择前插入

a'' - ''字符,所以我在考虑

首先接受字符串输入,然后添加'' - ''字符

我自己。


所以我的问题是,如何更改:

" 2a 3ab"进入-2a -3ab。


正则表达式? :/



s =" 2a 3b"

s =" - %s - %s"%tuple(s.split() )


-Larry


In< 11 **************** ******@r56g2000hsd.googlegroups .com> ;, kevinliu23

写道:


" 2a 3ab"进入-2a -3ab。



在[8]中:'' - ''+'' - ''。join(''2a 3ab 4xy''。split())

Out [8]:'' - 2a -3ab -4xy''


Ciao,

Marc''BlackJack''Rintsch


Hey guys,

I want to be able to insert a ''-'' character in front of all numeric
values in a string. I want to insert the ''-'' character to use in
conjunction with the getopt.getopt() function.

Rigt now, I''m implementing a menu system where users will be able to
select a set of options like "2a 3ab" which corresponds to menu
choices. However, with getopt.getopt(), it''ll only return what I want
if I input -2a -3ab as my string. I don''t want the user have to insert
a ''-'' character in front of all their choices, so I was thinking of
accepting the string input first, then adding in the ''-'' character
myself.

So my qusetion is, how do I change:

"2a 3ab" into "-2a -3ab".

Regular expressions? :/

解决方案

On Mar 30, 10:38 am, "kevinliu23" <kevinli...@gmail.comwrote:

Hey guys,

I want to be able to insert a ''-'' character in front of all numeric
values in a string. I want to insert the ''-'' character to use in
conjunction with the getopt.getopt() function.

Rigt now, I''m implementing a menu system where users will be able to
select a set of options like "2a 3ab" which corresponds to menu
choices. However, with getopt.getopt(), it''ll only return what I want
if I input -2a -3ab as my string. I don''t want the user have to insert
a ''-'' character in front of all their choices, so I was thinking of
accepting the string input first, then adding in the ''-'' character
myself.

So my qusetion is, how do I change:

"2a 3ab" into "-2a -3ab".

Regular expressions? :/

Regular expressions would definitely work. Here''s a hack though:

tempInput = ''2a 3ab''
tempLst = tempInput.split('' '')

output = ''''
for i in tempLst:
output += (''-'' + i + '' '')
I''m sure there are many better and more elegant hacks than this.

Mike


kevinliu23 wrote:

Hey guys,

I want to be able to insert a ''-'' character in front of all numeric
values in a string. I want to insert the ''-'' character to use in
conjunction with the getopt.getopt() function.

Rigt now, I''m implementing a menu system where users will be able to
select a set of options like "2a 3ab" which corresponds to menu
choices. However, with getopt.getopt(), it''ll only return what I want
if I input -2a -3ab as my string. I don''t want the user have to insert
a ''-'' character in front of all their choices, so I was thinking of
accepting the string input first, then adding in the ''-'' character
myself.

So my qusetion is, how do I change:

"2a 3ab" into "-2a -3ab".

Regular expressions? :/

s="2a 3b"
s="-%s -%s"% tuple(s.split())

-Larry


In <11**********************@r56g2000hsd.googlegroups .com>, kevinliu23
wrote:

"2a 3ab" into "-2a -3ab".

In [8]: ''-'' + '' -''.join(''2a 3ab 4xy''.split())
Out[8]: ''-2a -3ab -4xy''

Ciao,
Marc ''BlackJack'' Rintsch


这篇关于在字符串中的所有数字前面插入“ - ”字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆