473,386 Members | 1,721 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,386 software developers and data experts.

[newbie] regexp question

How do I write a regexp that will select 1.23 or -12.34 but not 1.2.3?

$a =~ s/(\-*\d+\.\d+)/foo\1bar/g;

This always selects 1.2 out of 1.2.3, and it should not. I.e., "foo1.2bar.3" is an error.

Thanks!
Jul 14 '07 #1
3 1007
numberwhun
3,509 Expert Mod 2GB
Maybe its because its after midnight and I have been coding all day, who knows, but I am not really sure what you are wanting to do.

That aside though, I took and modified your regex (only slightly), at the behest of the Komodo editor that I am using. Here is the quick script I wrote:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. print("Please input a string: ");
  5. my $input = <STDIN>;
  6. chomp($input);
  7.  
  8. $input =~ s/(\-*\d+\.\d+)/foo$1bar/g;
  9.  
  10. print("$input");
  11.  
Now, I ran the script a few times, using the input you provided. You can see here what the script produced:

######## Script Output ############

C:\>perl select.pl

Please input a string: foo1.2bar.3
foofoo1.2barbar.3

C:\>perl select.pl

Please input a string: 1.2.3
foo1.2bar.3

C:\>perl select.pl

Please input a string: 1.23
foo1.23bar

C:\>perl select.pl

Please input a string: -12.34
foo-12.34bar

C:\>

######## End Output #############

I addeed blank lines here manually for readability of the output. As you can see, I think it did what you wanted, but again, I wasn't quite sure what you were looking for.

If this isn't what you wanted, maybe you could elaborate a fair amount and provide better examples of what you are trying to achieve.

Regards,

Jeff
Jul 14 '07 #2
miller
1,089 Expert 1GB
I also do not know what you're ultimately trying to acheive. However, the following faq link will probably be helpful in some way.

perldoc perlfaq4 How do I determine whether a scalar is a number/whole/integer/float?

- Miller
Jul 14 '07 #3
KevinADC
4,059 Expert 2GB
you never told the regexp where to stop substituting, you only gave it part of a pattern to find and replace. If you don't want the rest of the patter/string included on the replacement side you have to add it to the search pattern and exclude it in the replacement pattern.

Expand|Select|Wrap|Line Numbers
  1. $a = '1.2.3';
  2. $a =~ s/(\-?\d+\.\d+)(?:\.\d)/foo\1bar/;
  3. print $a;
adding (?:\.\d) after the search pattern and not carrying it over to the replacement pattern (using $2 or \2) prevents the regexp from including that part of the string in the replacement pattern.
Jul 14 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: python_charmer2000 | last post by:
I want to match several regexps against a large body of text. What I have so far is similar to this: re1 = <some regexp> re2 = <some regexp> re3 = <some regexp> big_re = re.compile(re1 +...
19
by: Magnus Lie Hetland | last post by:
I'm working on a project (Atox) where I need to match quite a few regular expressions (several hundred) in reasonably large text files. I've found that this can easily get rather slow. (There are...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
2
by: Bill McCormick | last post by:
Hello, I'm new to VB.NET but have used regexp in Perl and VI. I'd like to read a regular expression from a file and apply it to a string read from another file. The regexp is simple word...
26
by: Matt Kruse | last post by:
Are there any current browsers that have Javascript support, but not RegExp support? For example, cell phone browsers, blackberrys, or other "minimal" browsers? I know that someone using Netscape...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
11
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
8
by: Darryl Kerkeslager | last post by:
Currently I am using the RegExp object to parse a large dataset in an Access table - but this table was exported from SQL Server, and the very correct question was asked - why not just do it in SQL...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.