It is necessary to find all the logins in the text.

String regex = "((?U)^(\\w+))"; does not work, finds only login in 1 line.

 Login;Name;Email ivanov;Ivan Ivanov;ivanov@mail.ru петров;Петр Петров;petrov@google.com obama;Barack Obama;obama@google.com bush;Джордж Буш;bush@mail.ru 
  • String regex = "(?Um)^\\w+"; or "(?Um)^[^\\s;]+" - Wiktor Stribiżew

2 answers 2

There is a simpler way of not using regulars.

 public class Main { public static void main(String[] args) { String text = "Login;Name;Email\n" + "ivanov;Ivan Ivanov;ivanov@mail.ru\n" + "петров;Петр Петров;petrov@google.com\n" + "obama;Barack Obama;obama@google.com\n" + "bush;Джордж Буш;bush@mail.ru"; String[] split = text.split("\n"); for (String s : split) { System.out.println(s.substring(0, s.indexOf(";"))); } } 

}

    If all the text is read into one string variable, you can force ^ find all positions after switching to a new line using Pattern.MULTILINE or (?m) :

     String regex = "(?Um)^\\w+"; 

    Note that brackets around the entire template are not needed. See the demo online .

    If you want to find any characters from the beginning of the line to the first ; use

     String regex = "(?Um)^[^\\s;]+"; 

    Template [^\\s;]+ will find 1 or more characters other than ; and whitespace characters at the beginning of a line.

    See the demo online .