Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6578749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:48:38+00:00 2026-05-25T15:48:38+00:00

I realise that this may seem like a stupid request but I’m going to

  • 0

I realise that this may seem like a stupid request but I’m going to ask anyway.

I wish to use a regular expression to find every nth comma in a list of numbers ie:

    88574,93243,129659,135504,136357,141052,141619,141619,142195,144622,144946,...

could then have every 4th comma ‘,’ replaced by ‘,\r\n’ thereby turning a list of numbers into a grid of 4 by n rows.

finding all commas was simple :

    [^0-9]

which from the above list will find all commas. How can I now group these matches to exclude three in every four.

I could do this with PHP preg_matches but I am using this with a mysql regular expression replacement function so would prefer a pure regex answer (if one exists).

The Function that I’m using in MySQL is below:

    DROP FUNCTION IF EXISTS `regex_replace`$$  

    CREATE DEFINER=`root`@`127.0.0.1`   
    FUNCTION `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original TEXT)   
    RETURNS VARCHAR(1000) CHARSET latin1  
        DETERMINISTIC  
    BEGIN  
     DECLARE temp VARCHAR(1000);  
     DECLARE ch VARCHAR(1);  
     DECLARE i INT;  
     SET i = 1;  
     SET temp = '';  
     IF original REGEXP pattern THEN  
     loop_label: LOOP  
       IF i>CHAR_LENGTH(original) THEN  
                 LEAVE loop_label;  
       END IF;  
       SET ch = SUBSTRING(original,i,1);  
         IF NOT ch REGEXP pattern THEN  
            SET temp = CONCAT(temp,ch);  
       ELSE  
          SET temp = CONCAT(temp,replacement);  
                END IF;  
       SET i=i+1;  
              END LOOP;  
     END IF;  
     RETURN temp;  
    END$$  

as you can see the regex it’s self does not have to handle complex matching. Therefore a regular expression which is capable of selecting the nth comma would be sufficient.

I hope this clarifies the problem.

Fin

EDIT:

I have added the lib_mysqludf_preg library to the serve which contains the preg_replace function. This is a PCRE implementation for MySQL and should work if I can answer the problem of the regex for selecting every-fourth ‘,’ and replace with ‘,\r\n’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:48:39+00:00Added an answer on May 25, 2026 at 3:48 pm
    $result = preg_replace('/(?:[^,]*,){4}/', '\0\r\n', $subject);
    

    This matches four comma-delimited values in a row (I’m assuming that you won’t have commas inside of strings within a group) and adds a CRLF after them.

    [EDIT]
    The above is a PHP based solution

    For a pure MySQL solution, install lib_mysqludf_preg and use:

        SELECT preg_replace('/(?:[^,]*,){4}/', '${0}\r\n', `fieldname`) as 'new_layout' from `tablename`;
    

    Many Thanks to all that contributed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realise that the title of this question may be inaccurate but I was
I realise that this question may be almost impossible to answer definitively, but: Is
This may seem like a really off-the-wall question, but I've been learning how Backbone.js
This may seem like a very basic question but I did not seem to
This question may seem blindingly obvious and I realise I am putting myself up
This question may be easy and the answer obvious, but I can't seem to
I realize that this question has been asked 100times but none that I have
While I realize that this question has been asked once or twice ago but
I asked this question in the restkit google group, but realize now that it
i want to realize a construct in MS SQL that would look like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.