Regular expression for finding the first character of every line

84 Views Asked by At

I need to find the first character of all the lines and remove it using Java.

Input lines:

Z301052023KDE12   0000.062500000 0000.000000000 0000.000000000
Z301052023KDE11   0000.000000000 0000.000000000 0000.000000000

Java code:

    public static void main(String args[])
    {
        

        String inString = "Z302052023CFCL11   0000.000000000 249999.990        0999999990000.000000000 0000.000000000 0000.000000000 0000.000000000\n"
         + "Z302052023CFCL11   0000.000000000 249999.990        0999999990000.000000000 0000.000000000 0000.000000000 0000.000000000" ;
        
        String value = null;

        inString = inString.replaceAll("^.", "");
        System.out.println(inString);
    }

Output:

302052023CFCL11   0000.000000000 249999.990        0999999990000.000000000 0000.000000000 0000.000000000 0000.000000000
Z302052023CFCL11   0000.000000000 249999.990        0999999990000.000000000 0000.000000000 0000.000000000 0000.000000000

As you can see the 2nd line's first character is not removed.

1

There are 1 best solutions below

3
blvc3 On BEST ANSWER

With Python:

import re

input_lines = '''
Z301052023KDE12 0000.062500000 0000.000000000 0000.000000000
Z301052023KDE11 0000.000000000 0000.000000000 0000.000000000
'''

pattern = '^.'

first_chars = re.findall(pattern, input_lines, flags=re.MULTILINE)
print(first_chars)

Result:

['Z', 'Z']