How can I increment a number matched via regex?

notepad++ regex increment number
regular expression increment number
increment numbers using regex
javascript regex increment number
java regex increment number
regex auto-increment
regex increment digit
vim replace with incrementing number

I am adding a new field in one of my web apps, for visitors to add a product. Pushing "add a product" clones one of the existing inputs, and I set it to a blank value.

I need to update the name however. The name is in this format:

<input type="text" name="product[0][0][3][title]" value="my product" id="input-AbGHtQS3" maxlength="150" />

I need to increment the last numerical index, that is the 3 in this example.

I wrote a regex to match the appropriate character, but how can I increment that last number?

Here is my regex:

/^product\[\d+\]\[\d+\]\[(\d+)\]\[.+\]/

How could I increment that last number?

from: Use RegExp to match a parenthetical number then increment it

The replace method can take a function as its second argument. It gets the match (including submatches) and returns the replacement string. Others have already mentioned that the parentheses need to be escaped.

"Item Name (4)".replace(/\((\d+)\)/, function(fullMatch, n) {
    return "(" + (Number(n) + 1) + ")";
});

So,

*edit:

this should work

"product[0][0][3][title]".replace(/(^product\[\d+\]\[\d+\]\[)(\d+)(\]\[.+\])/, function(fullMatch, n, a, o) {
    return n + (Number(a) + 1) + o;
});

Is it possible to increment numbers using regex substitution?, So this means match all digits that are followed by 9s until the '~' mark then go to the lookup table and find the digit following this number. Straight to the point, I'm wondering how to use grep/find/sed/awk to match a certain string (that ends with a number) and increment that number by 1. The closest I've come is to concatenate a 1 to the end (which works well enough) because the main point is to simply change the value.

A more iterative approach:

r = /^(product\[\d+\]\[\d+\]\[)(\d+)(\]\[[a-z]+\])$/;
m = s.match(r);
if (!m) { /* do something smart; */ }
s.replace(r, "$1" + (Number(m[2]) + 1) + "$3");

I added a capture group at the head and tail of your regex. Then match, increment, and reassemble the string.

+1 for the match function on the regex though, I didn't know that trick.

How do I do a increment in a regex match?, Don't. Regular expressions are string processors, and are excellent at pattern matching - but they don't have any concept of numbers, so the� In this example, we use a regex that matches one more characters between two numbers. It extracts a fragment of text that begins and ends with one or more digits. The first woman to win a Nobel Prize was Marie Curie, who won the Nobel Prize in Physics in 1903 with her husband, Pierre Curie, and Henri Becquerel.

Based on Andrew's answer, but corrected to return entire string:

"product[0][0][3][title]".replace (
    /^(product\[\d+\]\[\d+\]\[)(\d+)(\]\[.+\])/,
    function(fullMatch, pre, n, post) {
        return pre + (Number(n) + 1) + post;
    }
);

How to replace each match with incrementing counter?, This means that :let length+=1 increments a number, but doesn't return anything. By default, when we forgot to return something, 0 is returned -- it also The regular expression we're going to use will match the desired� How can I use the Matches activity to execute a regex expression that will return the values found. For example. The input string is something like "some text then (123) etc " I want 3 values back: the part before the parenthesis, the value in the parens and the value after

Incremental Regular Expressions, The problem of regular expression matching is a well-known one, with a multitude possible by greatly reducing the amount of code to be actually matched against the Increase the probability that this library will be indeed used and will not� Numeric value is matched by specified regular expression. It is possible to modify the regular expression to match specific numeric values. Note can contain free text (in this case only numeric part will be updated as per specified regular expression) It is required to add the text tag to the note in order to increment its value.

Need regex for incremental by 1, I wanted to replace with incremental by 1 in to the each value as create a two column table of incrementing numbers (it could be a long table,� You could match the numbers and the capital H using: \b(H )(\d+(?:\.\d+)?) \b(H ) Capture group 1, match H and a space preceded by a word boundary (Capture group 2 \d+(?:\.\d+)? Match 1+ digits, optionally match a dot and 1+ digits) Close group; Regex demo. In the replacement use the 2 capturing groups and add a number to what is captured in group 2.

[PDF] RegexMagic Manual, Actually incrementing the numbers requires arithmetic. Regular expressions don't do math. But. PowerGREP can do simple math on regex and group matches. Luckily we can modify the delimiters of the regexes in Perl 5 by using the letter m (which stand for matching) at the beginning. This way we can use many other characters instead of the slash. This way we can use many other characters instead of the slash.

Comments
  • why would you put what looks like a Javascript array in the name? Doing an eval on it later?
  • So I can work with it easily in PHP as an array
  • Didn't know you could give an anonymous function as 2nd argument! Learn something new every day. Thanks a bunch, will implement now.
  • Couldn't get it to work - see jsbin.com/obila/edit it is returning 4 - is that correct? How would I then make the name attribute read correctly? i.e. 'product[0][0][4][title]'
  • Ah crap, it's replacing the whole match. I'll give it another try.