Regex: move minus sign to front for negative numbers w traili ng " -"

Hi sir Guy,

that was very helpful and very quick, too (within a few min, i got a working
answer).

  1. I have “engraved” your name in “my” code (i will use the code in
    production to convert legacy apps output).
  2. I have to study more on ruby regex since (upon looking at your regex)
    some of it is new to me (and hoping someday I may help someone, too :slight_smile:

Again, many thanks.
-botp w small brain now learning…

···

-----Original Message-----
From: ts [mailto:decoux@moulon.inra.fr]
Sent: Saturday, July 27, 2002 7:57 PM
To: ruby-talk@ruby-lang.org
Cc: ruby-talk@ruby-lang.org
Subject: Re: regex: move minus sign to front for negative
numbers w trailing " -"

“P” == =?iso-8859-1?Q?=22Pe=F1a=2C Botp=22?=
writes:

99.99- 9999- 99-99 99-99- 99999.99- 9.99-

It really depend on what you want : for a regexp the most
difficult task is the specification. When you have a
complete specification, then you can try to write it. For
example I’ve added some cases

pigeon% cat b.rb
#!/usr/bin/ruby
DATA.each do |line|
puts line
puts
line.gsub(/(^|[^-.\d])(\d+(?:.\d+)?)-(?=[^-.\d]|$)/,’>>\1-\2<<’)
puts
end
END
99.99- 9999- 9.- .99- -99.99- 9.9-. 99-99 99-99-
99999.99- 9.99-
pigeon%

pigeon% b.rb
99.99- 9999- 9.- .99- -99.99- 9.9-. 99-99 99-99-
99999.99- 9.99-

-99.99<< >> -9999<< 9.- .99- -99.99- 9.9-. 99-99 99-99-
-99999.99<< >> -9.99<<

pigeon%

Guy Decoux