# Regular expression counter

**URL:** https://boards.straightdope.com/t/regular-expression-counter/526897
**Category:** Factual Questions
**Created:** [January 29, 2010, 3:19pm UTC](https://boards.straightdope.com/t/regular-expression-counter/526897 "2010-01-29T15:19:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Rune](https://avatars.discourse-cdn.com/v4/letter/r/e68b1a/32.png) [@Rune](https://boards.straightdope.com/u/Rune)
#### Post date: [January 29, 2010, 3:19pm UTC](https://boards.straightdope.com/t/regular-expression-counter/526897/1 "2010-01-29T15:19:00Z")

</div>

I want to replace the string “A B C D E F” with “A1 B2 C3 D4 E5” in a single regular expression. Is it possible to have a counter in regular expressions?

---

<div class="post-metadata">

### Author: ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)
#### Post date: [January 29, 2010, 3:56pm UTC](https://boards.straightdope.com/t/regular-expression-counter/526897/2 "2010-01-29T15:56:40Z")

</div>

Depends what flavor of regex you’re using. In Perl you can say:

```auto

my $s = "A B C D E F";
my $n = 0;
$s =~ s/(\w)/$1 .++$n/eg;

```

…because Perl allows you to execute arbitrary code in the replacement string with the /e flag. But there isn’t a way to do it with “standard” regex primitives that I can think of.

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [January 29, 2010, 4:01pm UTC](https://boards.straightdope.com/t/regular-expression-counter/526897/3 "2010-01-29T16:01:50Z")

</div>

You can’t do it with plain vanilla regular expressions, so you’ll need something like what **friedo** posted.

---

<div class="post-metadata">

### Author: ![Rune](https://avatars.discourse-cdn.com/v4/letter/r/e68b1a/32.png) [@Rune](https://boards.straightdope.com/u/Rune)
#### Post date: [January 29, 2010, 6:40pm UTC](https://boards.straightdope.com/t/regular-expression-counter/526897/4 "2010-01-29T18:40:50Z")

</div>

Ok. Thanks. It’s Javascript. I tried:

var i = 0;  
“A B C D E F”.replace(/ /g, ++i + " "));

- but i doesn’t get incremented before after all the replacements have been made. I now made a version:

var res = “”;  
var tmp = “A B C D E F”.split(/ /g);  
for (var i = 0; i \< tmp.length; i++) res += tmp\* + i + " ";

- not so nice.

---

<div class="post-metadata">

### Author: ![croc\_hunter](https://avatars.discourse-cdn.com/v4/letter/c/ed8c4c/32.png) [@croc\_hunter](https://boards.straightdope.com/u/croc_hunter)
#### Post date: [May 28, 2012, 9:54pm UTC](https://boards.straightdope.com/t/regular-expression-counter/526897/5 "2012-05-28T21:54:49Z")

</div>

Does anyone know why this reg exp in perl doenst work;

If there are two carriage returns in my string, this doens’t work.  
$message =~ s/

/\<p\>/g;

But if i use just one  
in the replace it does work…  
$message =~ s/  
/\<p\>/g;

any idea how i can serach for double  
's in string.?

---

<div class="post-metadata">

### Author: ![tellyworth](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tellyworth](https://boards.straightdope.com/u/tellyworth)
#### Post date: [May 28, 2012, 10:23pm UTC](https://boards.straightdope.com/t/regular-expression-counter/526897/6 "2012-05-28T22:23:21Z")

</div>

Your question is unrelated to the first in the thread.

Nevertheless: your string probably has DOS-style CRLF line ends. Try

instead of

.
