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

(Perl/Regex) Match a Repeated Group of Characters

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

✓ answered by RonB

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+)/ 

1 1622
RonB
589 Expert Mod 512MB
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.