Skip to content

Instantly share code, notes, and snippets.

@ezhov-da
Last active March 10, 2019 12:16
Show Gist options
  • Save ezhov-da/7c581f77af99f4d016a26bb5edd2ec42 to your computer and use it in GitHub Desktop.
Save ezhov-da/7c581f77af99f4d016a26bb5edd2ec42 to your computer and use it in GitHub Desktop.
пример использования метода String.replaceAll
import java.util.logging.Logger;
/**
* Created by ezhov_da on 13.03.2018.
*/
public class ReplaceAllTest {
private static final Logger LOG = Logger.getLogger(ReplaceAllTest.class.getName());
public static void main(String[] args) {
/*
*Сегодня увидел достаточно интересное применение метода String.replaceAll, а именно:
*в методе replaceAll, в аргументе 'replacement', можно сослаться на группу из регулярного выражения.
*/
String firstExample = "Hello, user 12345, please go out!";
String firstResult = firstExample.replaceAll(".+\\s(\\d+),?.+", "$1");
System.out.println(firstResult); //12345
String secondExample = "Hello, user 12345, please go out from this!";
String secondResult = secondExample.replaceAll(".+\\s(\\d+),?.+\\s(this)", "$1 - $2");
System.out.println(secondResult); //12345 - this!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment