# Regex returning less number of groups - where is the error?

**URL:** <https://www.ruby-forum.com/t/regex-returning-less-number-of-groups-where-is-the-error/162950>\
**Category:** Ruby\
**Created:** [March 16, 2009, 3:13am UTC](https://www.ruby-forum.com/t/regex-returning-less-number-of-groups-where-is-the-error/162950 "2009-03-16T03:13:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![knakahara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3ab097/32.png) [@knakahara](https://www.ruby-forum.com/u/knakahara)\
**Post date:** [March 16, 2009, 3:13am UTC](https://www.ruby-forum.com/t/regex-returning-less-number-of-groups-where-is-the-error/162950/1 "2009-03-16T03:13:16Z")

</div>

Hello!

I’m trying to build a regex to count the number of modified files by  
commit. For example, for the following svn log output:

* * *

r727998 | bentmann | 2008-12-19 21:50:48 +1100 (Fri, 19 Dec 2008) | 4  
lines  
Changed paths:  
A  
/maven/components/branches/maven-2.0.x/maven-core/src/main/resources/org/apache/maven/messages/messages\_de.properties  
M  
/maven/components/branches/maven-2.0.x/maven-core/src/main/resources/org/apache/maven/messages/messages\_en.properties

[MNG-3451] [MNG-3790] German localization for maven-core  
Submitted by: Christian Schulte

## o Applied with minor modifications

r727871 | brett | 2008-12-19 11:48:07 +1100 (Fri, 19 Dec 2008) | 2 lines  
Changed paths:  
M /maven/components/branches/maven-2.0.x  
M  
/maven/components/branches/maven-2.0.x/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java

[MNG-1830] use ISO 8601 format (not combined for readability)

* * *

It need to return the revision number and the number of modified files,  
so the result of match would be something like :

Result 1:  
1. r727871 | brett | 2008-12-19 11:48:07 +1100 (Fri, 19 Dec 2008) |  
2 lines  
2. A  
/maven/components/branches/maven-2.0.x/maven-core/src/main/resources/org/apache/maven/messages/messages\_de.properties  
3. M  
/maven/components/branches/maven-2.0.x/maven-core/src/main/resources/org/apache/maven/messages/messages\_en.properties"]  
Result 2:  
1. r727871 | brett | 2008-12-19 11:48:07 +1100 (Fri, 19 Dec 2008) |  
2 lines  
2. M /maven/components/branches/maven-2.0.x  
3. M  
/maven/components/branches/maven-2.0.x/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java

I tried many variations of the following regular expression:

/(^r\d+._?)(?:^Changed paths:\n)(^\s_[MDA]\s(?:/[\w.-]+)+/m

but either it returns incomplete results, as:

Result 1

1. r727998 | bentmann | 2008-12-19 21:50:48 +1100 (Fri, 19 Dec 2008) | 4  
lines
2. A  
/maven/components/branches/maven-2.0.x/maven-core/src/main/resources/org/apache/maven/messages/messages\_de.properties

Result 2

1. r727871 | brett | 2008-12-19 11:48:07 +1100 (Fri, 19 Dec 2008) | 2  
lines
2. M /maven/components/branches/maven-2.0.x

Would someone be able to help me on this? Thanks in advance!!

Kimie

---

<div class="post-metadata">

**Author:** ![knakahara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3ab097/32.png) [@knakahara](https://www.ruby-forum.com/u/knakahara)\
**Post date:** [March 16, 2009, 4:05am UTC](https://www.ruby-forum.com/t/regex-returning-less-number-of-groups-where-is-the-error/162950/2 "2009-03-16T04:05:11Z")

</div>

Hi,

2009/3/16 Kimie N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> /maven/components/branches/maven-2.0.x/maven-core/src/main/resources/org/apache/maven/messages/messages\_de.properties  
> Â M /maven/components/branches/maven-2.0.x  
> Result 1:  
> Â Â 3. M
> 
> 1. r727998 | bentmann | 2008-12-19 21:50:48 +1100 (Fri, 19 Dec 2008) | 4  
> Would someone be able to help me on this? Thanks in advance!!

I guess you want this:  
/(^r\d+._?)(?:^Changed paths:\n)((?:^\s_[MDA]\s/[\w./-]+\n)+)/m

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![knakahara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3ab097/32.png) [@knakahara](https://www.ruby-forum.com/u/knakahara)\
**Post date:** [March 16, 2009, 4:51am UTC](https://www.ruby-forum.com/t/regex-returning-less-number-of-groups-where-is-the-error/162950/3 "2009-03-16T04:51:12Z")

</div>

Hi Park, thank you for the quick answer. Using your regex, I get the  
following result:

Result 1

1. r727998 | bentmann | 2008-12-19 21:50:48 +1100 (Fri, 19 Dec 2008) | 4  
lines
2. A  
/maven/components/branches/maven-2.0.x/maven-core/src/main/resources/org/apache/maven/messages/messages\_de.properties  
M  
/maven/components/branches/maven-2.0.x/maven-core/src/main/resources/org/apache/maven/messages/messages\_en.properties

Result 2

1. r727871 | brett | 2008-12-19 11:48:07 +1100 (Fri, 19 Dec 2008) | 2  
lines
2. M /maven/components/branches/maven-2.0.x  
M  
/maven/components/branches/maven-2.0.x/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java

I returns all modified lines, but in the same group. So, for example,  
to know how many files were modified in revision 727998, I would have to  
work on the result[0][1] and result[1][1] ( (or 1.2 and 2.2 as in  
example above) to extract the number of modified files. Maybe splitting  
the text and counting how many files were modified (1 per line).  
Actually I tried to do it splitting by \n, but it didn’t work. So what I  
was wondering if it possible to return each modified line as a diferent  
group, as I said in the first email.

Or, if you can suggest a way to break the lines of results[0][1] and  
results[1][1], it would make it too!

Thank you!  
Kimie

---

<div class="post-metadata">

**Author:** ![knakahara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3ab097/32.png) [@knakahara](https://www.ruby-forum.com/u/knakahara)\
**Post date:** [March 16, 2009, 5:47am UTC](https://www.ruby-forum.com/t/regex-returning-less-number-of-groups-where-is-the-error/162950/4 "2009-03-16T05:47:27Z")

</div>

2009/3/16 Kimie N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> /maven/components/branches/maven-2.0.x/maven-core/src/main/resources/org/apache/maven/messages/messages\_en.properties  
> to know how many files were modified in revision 727998, I would have to  
> work on the result[0][1] and result[1][1] ( (or 1.2 and 2.2 as in  
> example above) to extract the number of modified files. Â Maybe splitting  
> the text and counting how many files were modified (1 per line).  
> Actually I tried to do it splitting by \n, but it didn’t work. So what I  
> was wondering if it possible to return each modified line as a diferent  
> group, as I said in the first email.
> 
> Or, if you can suggest a way to break the lines of results[0][1] and  
> results[1][1], it would make it too!

Or try with the more simple regex:  
/(^r\d+._?)(?:^Changed paths:\n)|(^\s_[MDA]\s/[\w./-]+\n)/m

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![knakahara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3ab097/32.png) [@knakahara](https://www.ruby-forum.com/u/knakahara)\
**Post date:** [March 17, 2009, 5:56am UTC](https://www.ruby-forum.com/t/regex-returning-less-number-of-groups-where-is-the-error/162950/5 "2009-03-17T05:56:05Z")

</div>

Yep, the simpler regex does the job. Thanks a lot!
