Last active
March 10, 2019 12:16
-
-
Save ezhov-da/7c581f77af99f4d016a26bb5edd2ec42 to your computer and use it in GitHub Desktop.
пример использования метода String.replaceAll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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