Regular expression match all except first occurence

gabrielgeo

I need a regular expression to match all occurrences of a dot (.) except the first one.

For example if the source is: aaa.bbb.ccc..ddd

the expression should match the dots after bbb and ccc but not the dot after aaa. In other works it should match all dots except the first one.

I need it for javascript regex.

Casimir et Hippolyte

with pcre (PHP, R) you can do that:

\G(?:\A[^.]*\.)?+[^.]*\K\.

demo

details:

\G # anchor for the start of the string or the position after a previous match
(?:\A[^.]*\.)?+ # start of the string (optional possessive quantifier)
[^.]* # all that is not a dot
\K    # remove all that has been matched on the left from the match result
\.    # the literal dot

With .net: (easy since you can use a variable length lookbehind)

(?<!^[^.]*)\.

demo


With javascript there is no way to do it with a single pattern.

using a placeholder:

var result = s.replace('.', 'PLACEHOLDER')
              .replace(/\./g, '|')
              .replace('PLACEHOLDER', '.');

(or replace all dots with | and then replace the first occurrence of | with a dot).

using split:

var parts = s.split('.');
var result = parts.shift() + (parts.length ? '.': '') + parts.join('|');

with a counter:

var counter = 0;
var result = s.replace(/\./g, (_) => counter++ ? '|' : '.');

With NodeJS (or any other implementation that allows lookbehinds):

var result = s.replace(/((?:^[^.]*\.)?(?<=.)[^.]*)\./g, "$1|");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Regular expression except first

Regular expression match last occurence of year in string

Regular expression: Replace everything before first occurence

Regular expression to match all words except those containing a certain pattern

R - Regular expression to match all punctuation except that inside of a URL

replace a string in all occurence using regular expression

Regex to match every occurence of a character except the first one

R - regular expression, match after second or third occurence

Regular expression to stop at first match

Regular expression to match first element

Regular expression to match everything except two strings?

Regular expression to match everything, except HTML tags

Regular expression Javascript match letters except one

How to match all the non numeric characters except a plus symbol using regular expression?

Regular expression to match all 4XX numbers except 400 and 411

Regular expression to match all digits of unknown length except the last 4 digits

regular expression to match all except the last 2 strings in every line <python>

How to replace all occurrences of a character except the first one in PHP using a regular expression?

Regular expression which allows all characters except for "<>_ ;{}[]"

Regex match all except first instance

Regular expression to get the first match in a text file

Regular Expression in BigQuery Match the First and Middle String

Regular expression not progressing after first match

Regular expression to match first and last character

Regular Expression Match only first character

Regular expression only finding first match

Regular expression stop after first match

Is regular expression search guaranteed to return first match?

Regular expression to match the first appearance of a character