Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Match multiple substrings and concatenate

Given I have

KsKd2s4dAs =AhAd2s4dKs =AsAd2s4dKs 5s5d6s6d2c AsAd6s6d2c =AdQdKdJdTd =AhQhKhJhTh =AsQsKsJsTs

I want to match all the substrings starting with '=' and concatenate them with this sign.

Output should be

KsKd2s4dAs AhAd2s4dKs=AsAd2s4dKs 5s5d6s6d2c AsAd6s6d2c AdQdKdJdTd=AhQhKhJhTh=AsQsKsJsTs

I am able to capture by using this regexp (=.{10}\s?)+ but failing to find a convenient way to make such string.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use

preg_replace('~(?:\G(?!\A)\s+(?==)|(?<!\S)=)(\S+)~', '$1', $text)

See the regex demo. Details:

  • (?:\G(?!\A)\s+(?==)|(?<!\S)=) - either a = char at the start of string or right after a whitespace or the end of the previous match and then one or more whitespaces followed with a = char
  • (\S+) - Group 1: one or more non-whitespace chars.

Another way is using preg_replace_callback:

$text = 'KsKd2s4dAs =AhAd2s4dKs =AsAd2s4dKs 5s5d6s6d2c AsAd6s6d2c =AdQdKdJdTd =AhQhKhJhTh =AsQsKsJsTs';
echo preg_replace_callback('~=(\S+(?:\s+=\S+)*)~', function($m) {
    return preg_replace('~\s+~', '', $m[1]);
}, $text);

See the PHP demo and the regex demo.

The =(\S+(?:\s+=\S+)*) matches a = and then captures into Group 1 any one or more non-whitespaces followed with zero or more repetitions of one or more whitespaces, =, and one or more non-whitespaces.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!