473,700 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

37 New Member
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 2324
numberwhun
3,509 Recognized Expert Moderator Specialist
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
2210
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 = " s^\?AAA\?01^BBB^g; #Comment "
1
3495
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 followed by the words AS. This is coming from an SQL select statement. Basically, I'm looking to match all the field names in an SQL select
3
18750
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". Therefore some matches are: XYZAHIJ ABC
4
2148
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 href="wap://xxx.xxx.xxx" xxx>xxx xxx<a href="http://xxx.xxx.xxx" xxx>xxx ..... And I want to find all the "http://xxx.xxx.xxx" out, so I do it
5
2842
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 from these lines. Question If the object I extract is in the form of a number how can I cast it into a number so I can prerform mathematical operations on it ?
17
11652
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"; var newString = myString.replace(pattern,'gh'); alert(newString) Gives me: asghasghasghasgh as it should. What I want: asghasghasghasdf Where the last one is not replaced.
2
1927
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
1537
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 (?:($Addr_Match{direct})\W+ (?{ $_{street} = $^N }) ($Addr_Match{type})\b (?{ $_{type} = $^N })) | (?:($Addr_Match{direct})\W+ (?{ $_{prefix} = $^N }))?
8
23244
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) total length must be between 9 and 12 chars, a valid example would be "123A456789" I can enforce the first two rules by using this:
2
7736
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
8714
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8641
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8958
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8912
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7797
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2379
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2021
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.