473,396 Members | 2,076 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,396 software developers and data experts.

Perl/Regular Expression how to match any number of words followed by a number?

Hello,

I am new to regular expression and wonder if someone could please help me with matching any number of words followed by a number.

I've tried the following :

$str = "aaa bbb ccc -45";
while ($str =~ m/(\w+)+|-?\d{1,2}$/g) {
print $1;
}

And $1 only picked up "aaa", "bbb", "ccc" but not "-45".
I appreciate any help anyone can post.

Thank you,

Akino
May 22 '12 #1
1 2320
numberwhun
3,509 Expert Mod 2GB
The reason the -45 was not caught, and I am speculating a bit, is because the - sign is part of the 45 and its not specified anywhere in your regex. Just a thought though.

I did some quick coding and came up with the following, which does match, although I haven't tested it on a number without a "-". Feel free though.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $str = "aaa bbb ccc -45";
  7.  
  8. if($str =~ /((\w+\s+)+\-*\d*)$/){
  9.     print("$1\n");
  10. }
  11.  
Also, always use strict and warnings. They are your friends. :)

Regards,

Jeff
Jun 21 '12 #2

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

Similar topics

4
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " ...
1
by: Eric B. | last post by:
Hi, I'm hoping someone can help me come up with a regular expression that I need to match the following. I'm looking to match all occurances of format <word>.<word> in a string that is not...
3
by: Derek Stone | last post by:
In my continuing inability to completely understand regular expressions I have a new one for you. I'd like to capture a string "A" unless it is anywhere in between string "B" and string "C". ...
4
by: Johnny Lee | last post by:
Hi, I've met a problem in match a regular expression in python. Hope any of you could help me. Here are the details: I have many tags like this: xxx<a href="http://xxx.xxx.xxx" xxx>xxx xxx<a...
5
by: Paul Johnston | last post by:
Hi Just started using c# to do a task I usually use Perl to do and hit a limit in my knowledge :-) I have a program which reads from a large text file, extracts certain lines then gets a string...
17
by: Randy Webb | last post by:
I know that the /g flag will match all occurrences. Is there a way, with a Regular Expression, to match all occurrences *except* the last one? pattern = /df/g; var myString = "asdfasdfasdfasdf";...
2
by: teo | last post by:
match word with interpunctuation Hallo, I need to build a regular expression able to match a specified word, preceded and followed by few chars (mainly interpunctuation) Below the code. ...
3
by: John Nagle | last post by:
Here's a large Perl regular expression, from a Perl address parser in CPAN: use re 'eval'; $Addr_Match{street} = qr/ (?: # special case for addresses like 100 South Street...
8
by: Sharkie | last post by:
I need a regular expression to evaluate a text input field. The validation rules are: 1) only A-Z and 0-9 chars allowed, 2) only one alpha char allowed (at any position) - rest are digits, 3)...
2
by: Sal Sal | last post by:
If I have a string as follows XXXasdf23s5\r\n asdflkoirfn329i4\r\n sef29384ewrj28039\r\n XXX123sd3t334\r\n sdorfu23984rr\r\n sdflk2893rjf\r\n weirj2983jhwer2398\r\n XXX12356789\r\n
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.