5

How can I find middle character with regex only

For example,this shows the expected output

Hello -> l

world -> r

merged -> rg (see this for even number of occurances)

hi -> hi

I -> I

I tried

(?<=\w+).(?=\w+)
NAMO
  • 263
  • 3
  • 12

4 Answers4

5

Regular expressions cannot count in the way that you are looking for. This looks like something regular expressions cannot accomplish. I suggest writing code to solve this.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
  • 1
    +1 - So far, this is the only Answer that actually answers the question. The OP specifically wants to do it using a regex ... to understand the limits of regexes. – Stephen C Jun 26 '13 at 08:44
1
String str="Hello";
String mid="";
int len = str.length();
if(len%2==1)
    mid= Character.toString(str.getCharAt(len/2));
else
    mid= Character.toString(str.getChatAt(len/2))+ Character.toStringstr.getCharAt((len/2)-1));

This should probably work.

Veer Shrivastav
  • 5,434
  • 11
  • 53
  • 83
1
public static void main(String[] args) {
        String s = "jogijogi";
        int size = s.length() / 2;
        String temp = "";
        if (s.length() % 2 == 0) {
            temp = s.substring(size - 1, (s.length() - size) + 1);
        } else if (s.length() % 2 != 0) {
            temp = s.substring(size, (s.length() - size));
        } else {
            temp = s.substring(1);

        }

        System.out.println(temp);
    }
shreyansh jogi
  • 2,082
  • 12
  • 20
0

Related: How to match the middle character in a string with regex?


The following regex is based on @jaytea's approach and works well with e.g. PCRE, Java or C#.

^(?:.(?=.+?(.\1?$)))*?(^..?$|..?(?=\1$))

Here is the demo at regex101 and a .NET demo at RegexPlanet (click the green ".NET" button)

Middle character(s) will be found in the second capturing group. The goal is to capture two middle characters if there is an even amount of characters, else one. It works by a growing capture towards the end (first group) while lazily going through the string until it ends with the captured substring that grows with each repitition. ^..?$ is used to match strings with one or two characters length.

This "growing" works with capturing inside a repeated lookahead by placing an optional reference to the same group together with a freshly captured character into that group (further reading here).

A PCRE-variant with \K to reset and full matches: ^(?:.(?=.+?(.\1?$)))+?\K..?(?=\1$)|^..?


Curious about the "easy solution using balancing groups" that @Qtax mentions in his question.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46