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

  • Home
  • SEARCH
  • 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 7827589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:56:58+00:00 2026-06-02T09:56:58+00:00

I’m trying to figure out a preg_match / php style regex to find repeating

  • 0

I’m trying to figure out a preg_match / php style regex to find repeating groups of alphanumeric characters(of any length), separated by commas?

so if I have string “c,b,a,xz,x,b,a,c,xz,x,x,b,a”

would return the first series of letters that repeat more than two values. I think I need to do a recursive backreference, maybe something like

<?php
    // lines removed for simplicity
    // test string = "c,b,a,xz,x,b,a,c,xz,x,x,b,a"
    $haystack = "c,b,a,xz,x,b,a,c,xz,x,x,b,a";
    $answer = preg_match('/([A-z]{2,*}[\s]{1})([A-z \s]*)[\1]*/', $haystack );

    echo $answer; // print the first occurrence of the repeating series of two or more
?>

I just need to find and echo out the first occurrence of a repeating series of two or more values. Is there a way to use a backreference recursively, or some better method?

edit: code vomit removed.

  • 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-06-02T09:56:59+00:00Added an answer on June 2, 2026 at 9:56 am

    '/\b(\w+,\w+),(?:.*,)?\1\b/' should work. It’d match any sequence of two items, any amount of other stuff, and then the same sequence again.

    Catch is, it will likely find the first sequence that has a duplicate, not the sequence that has the first duplicate, due to how regexes work. (The match that starts earliest, wins.) For example, if you have 'a,b,c,d,c,d,a,b,c', $matches[1] would probably be 'a,b', even though 'c,d' would match earlier.

    To find the first duplicate, you’d have to be able to match that and have a backreference to it in a lookbehind assertion. If that’s even legal (which i doubt it is), it’d have to be fixed width before PHP would let it happen.

    Edit:
    Although, now that i think about it…if you reversed the string and then used '/.*\b(\w+,\w+),(?:.*?,)??\1\b/' on that, it might work. That dances around the constraint i’d mentioned; with the string reversed, the duplicate comes before the original, so now we can match the duplicate and then refer to it “later”.

    The .* at the beginning of the expression grabs as much as it can, so the match will start as close to the end of the reversed string (and therefore, as close to the beginning of the original string) as possible. And the extra ?s make their corresponding bits lazy, so they match as little as necessary. Of course, once you find the match in the reversed string, you’ll need to reverse it in order to get the match in the original string.

    And of course, this could break all to hell in the presence of UTF-8. (Then again, most regexes would.) If you’re just dealing with ASCII, though, it should work.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.