Regex replace part of string with spaces

It seems simple, but I can't get it to work.

I have a line that looks like “NNDDDDDAAAA”, where “N” is not a digit, “D” is a digit, and “A” is anything. I need to replace each character A with a space. The number of "N", "D" and "A" in the input line is always different.

I know how to do this with two expressions. I can split the line into two, and then replace everything in the second group with spaces. Like this

    Pattern pattern = Pattern.compile("(\\D+\\d+)(.+)");
    Matcher matcher = pattern.matcher(input);
    if (matcher.matches()) {
        return matcher.group(1) + matcher.group(2).replaceAll(".", " ");
    }

But I was wondering if this is possible with a single regex expression.

+3
source share
4 answers

, , ? :

StringBuilder sb = new StringBuilder(inputString);
for (int i = sb.length() - 1; i >= 0; i--) {
    if (Character.isDigit(sb.charAt(i)))
        break;
    sb.setCharAt(i, ' ');
}
String output = sb.toString();

. , , - , , . , .

0

, , NNDDDDD A N, A, DDDDD AAAA.

, NNDDDDDNAAA, NAAA . , : (\\D+\\d+)(\\D.+)

lookbehind Java ; + *. . , {1,9} +, 1 9 : (?<=\\D{1,9}\\d{1,9})(\\D.+)

, NAAA , "NNNDDDDNAAA".replaceAll("(?<=\\D{1,9}\\d{1,9})(\\D.+)", " ") NAAA , .

, , . , ; .

, StringBuilder StringBuffer . NNDDDDDAAAAA, StringBuilder, .

class Test {

public static Pattern p = Pattern.compile("(\\D+\\d+)(\\D.+)");

public static StringBuffer replace( String input ) {
    StringBuffer output = new StringBuffer();
    Matcher m = Test.p.matcher(input);
    if( m.matches() )
        output.append( m.group(1) ).append( m.group(2).replaceAll("."," ") );

    return output;
}

public static void main( String[] args ) {
    String input = args[0];
    long startTime;

    StringBuffer tests = new StringBuffer();
    startTime = System.currentTimeMillis();
        for( int i = 0; i < 50; i++)
        {
            tests.append( "Input -> Output: '" );
            tests.append( input );
            tests.append( "' -> '" );
            tests.append( Test.replace( input ) );
            tests.append( "'\n" );
        }
    System.out.println( tests.toString() );
    System.out.println( "\n" + (System.currentTimeMillis()-startTime));
}

}

Update: . 4-5 .

public static StringBuffer replace( String input )
{
    StringBuffer output = new StringBuffer();
    boolean second = false, third = false;
    for( int i = 0; i < input.length(); i++ )
    {
        if( !second && Character.isDigit(input.charAt(i)) )
            second = true;

        if( second && !third && Character.isLetter(input.charAt(i)) )
            third = true;

        if( second && third )
            output.append( ' ' );
        else
            output.append( input.charAt(i) );

    }

    return output;
}
+3

nondigit vs anything?

[^a-zA-Z0-9]
matches the letter or number not .

you want to replace anything that matches the regex above with a space.

Is that what you were talking about?

+1
source

You want to use positive appearance to match N and D, and then use normal match for A.

Not sure about a positive look at Java grammar, but in some article about Java regex with look

+1
source

Source: https://habr.com/ru/post/1712154/


All Articles