473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inserting '-' character in front of all numbers in a string

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? :/

Mar 30 '07 #1
10 2637
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

Mar 30 '07 #2
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
Mar 30 '07 #3
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
Mar 30 '07 #4
Hey guys, thanks for the quick replies. I'm looking for something more
generic than adding it to "2a 3ab". For example, under the menu option
2, there can be upwards of 8 other suboptions. I'll see what's
suggested here and post back if I run into more problems. Thanks guys!

Mar 30 '07 #5

On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:
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".
Will the first character always be a digit?

for i in yourstring.split():
if i[0].isdigit():
yourstring = yourstring.replace(i, '-%s' % (i,))

Or are these hex numbers?

Mar 30 '07 #6
On Mar 30, 2:09 pm, Michael Bentley <mich...@jedimindworks.comwrote:
On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:


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".

Will the first character always be a digit?

for i in yourstring.split():
if i[0].isdigit():
yourstring = yourstring.replace(i, '-%s' % (i,))

Or are these hex numbers?- Hide quoted text -

- Show quoted text -
Your replace strategy is risky. If:

yourstring = "1ab 2bc 3de 11ab"

it will convert to

-1ab -2bc -3de 1-1ab

-- Paul
Mar 30 '07 #7
Paul McGuire wrote:
On Mar 30, 2:09 pm, Michael Bentley <mich...@jedimindworks.comwrote:
>>On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:

>>>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.
' '.join(map(lambda x: '-' + x, s.split()))

assuming that you just want to put a "-" in front of each field,
regardless of its content.

John Nagle
Mar 30 '07 #8

On Mar 30, 2007, at 4:41 PM, Paul McGuire wrote:
On Mar 30, 2:09 pm, Michael Bentley <mich...@jedimindworks.comwrote:
>On Mar 30, 2007, at 10:38 AM, kevinliu23 wrote:


>>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".

Will the first character always be a digit?

for i in yourstring.split():
if i[0].isdigit():
yourstring = yourstring.replace(i, '-%s' % (i,))

Or are these hex numbers?- Hide quoted text -

- Show quoted text -

Your replace strategy is risky. If:

yourstring = "1ab 2bc 3de 11ab"

it will convert to

-1ab -2bc -3de 1-1ab
True enough! Good catch! How's this?

for i in yourstring.split():
if i[0].isdigit():
yourstring = yourstring.replace(i, '-%s' % (i,), 1)
Mar 30 '07 #9

On Mar 30, 2007, at 5:42 PM, Michael Bentley wrote:
for i in yourstring.split():
if i[0].isdigit():
yourstring = yourstring.replace(i, '-%s' % (i,), 1)
*OR*

yourstring ' '.join(x[0].isdigit() and '-%s' % x or x for x in
yourstring.split())

;-)
Mar 30 '07 #10
On 30 Mar 2007 08:38:27 -0700, kevinliu23 <ke********@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.
....
"2a 3ab" into "-2a -3ab".
Are you sure you want getopt, or are you just reusing it because you
don't know enough about string parsing and REs?

-2a -3ab is a bit limited: if you run out of digits and have to use
10, 11, ... then getopt will treat '-10ab' as '-1' without argument
and -0 with 'ab' as argument. It will probably choke on the
argumentless -1, too.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Apr 3 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Miranda | last post by:
Hi, I have a ASP/vbscript program that generates random passwords. The problem is I need to insert those passwords into an Access database of 327 clients. I have the random password program...
3
by: J. Muenchbourg | last post by:
while inserting new records into SQL, i'm using the folloinwg sqlstatement> Dim MySQL as string = "Insert into roster (pname, pnotes, thedate) values (@pname, @pnotes, @thedate)" the sql...
7
by: Dave Ohlsson | last post by:
Hi, In ISO C/C++, a string constant prefixed by the letter `L' is a wide string constant and is of type "array of wchar_t". Consider the following C program fragment: #include <stdio.h>...
10
by: M Bourgon | last post by:
I'm trying to figure out how to find the last whitespace character in a varchar string. To complicate things, it's not just spaces that I'm looking for, but certain ascii characters (otherwise,...
7
by: Astra | last post by:
Hi All Can you please help me with a few queries on adding a header line and padding rows out. I apologise profusely for not providing the DDL for this, but I don't have it. All I have is...
15
by: Jaraba | last post by:
I am working in a project that I need to parse an arrayt an select records based upon the values parsed. I used the functions developed by Knut Stolze in his article 'Parsing Strings'. I am...
5
by: Steve | last post by:
I'm getting string returned from my Database with a space in front of it, it sometimes it looks like this 8 9 10 1 and so one, how can I remove the white space in front of the numbers? Its...
5
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @...
5
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.