473,473 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

(Perl/Regex) Match a Repeated Group of Characters

37 New Member
Hello Everyone,

Could you please help me with one question? I have a line of text :

akinot:x:1240:1240:Tomeri Akino

And I would like to capture 'akinot' in $1, 'x:1240:1240:' in $2, and 'Tomeri' in $3

If I write :
Expand|Select|Wrap|Line Numbers
  1.      $line =~ /(\w+):((\w+:){3})(\w+)/
  2.  
then $1 is correct ('akinot'), $2 is correct ('x:1240:1240:'). But $3 is '1240:' instead of 'Tomeri'. Could you please tell me what I am doing wrong?

Thank you,
Akino
Dec 23 '14 #1
1 1625
RonB
589 Recognized Expert Moderator Contributor
Your repeated group match is using 2 sets of capturing parens which causes your regex to return 4 matches, not your expected 3 matches. To fix that you need to make the inner pair non-capturing.

Expand|Select|Wrap|Line Numbers
  1.  /^(\w+):((?:\w+:){3})(\w+)/ 
Dec 24 '14 #2

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

Similar topics

9
by: Leif K-Brooks | last post by:
How do I make a regular expression which will match the same character repeated one or more times, instead of matching repetitions of any (possibly non-same) characters like ".+" does? In other...
0
by: Lee Kuhn | last post by:
Is there a way to add a literal character to a regex match? For example, normally applying the regex ..$ to the string "123456" results in a match "56". I want to be to add a literal character to...
3
by: Jeff McPhail | last post by:
I am using Regex.Match in a large application and the memory is growing out of control. I have tried several ways to try and release the memory and none of them work. Here are some similar examples...
1
by: DKode | last post by:
I have the following regex that I am using: Match match = Regex.Match(global, "%(?<column>.+?)%"); my input string (global) is this: "something\%username%\somethingelse\%match2%\some\%match3%...
8
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a...
3
by: Hrvoje Niksic | last post by:
I often have the need to match multiple regexes against a single string, typically a line of input, like this: if (matchobj = re1.match(line)): ... re1 matched; do something with matchobj ......
1
by: Maurizio Vitale | last post by:
Has to be something really stupid, but the following never finish (running Python 2.5.1 (r251:54863, Jan 10 2008, 18:00:49) on linux2). The intention is to match C++ identifiers, with or...
0
by: MRAB | last post by:
Steve Holden wrote: Nor Perl. Probably what you want is re.findall(r"(\d)", "123456"), which returns a list of what it captured.
0
by: scsoce | last post by:
MRAB wrote: Yes, you are right, but this way findall() capture only the 'top' group. What I really need to do is to capture nested and repated patterns, say, <tabletag in html contains many <tr>, ...
8
by: user1980 | last post by:
hi there it seems to be simple but i do not know why my java script function does not work. function validatePartialDateInput(SourceField, FieldLabel) { var sField =...
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
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,...
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...
1
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.