Regex substitution: getting half of the string

I got another question about regex. The requirement is quite easy:

Given a string that has length of a even number.

  • 12
  • 1234
  • 123456
  • 12345678
  • abcdef

Write a substition regex to get the first half of the string:

After substition:

  • 1
  • 12
  • 123
  • 1234
  • abc

I'm using pcre, it supports recursion and control verbs.

I tried something like this but it's not working :(


Here's the test subject on regex101

Is it possible? How can I achieve this?

Read more here:

Content Attribution

This content was originally published by Hao Wu at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: