我有一个类似“portal100common2055”
的字符串。
我想把它分成两部分,第二部分应该只包含数字。
“portal200511sbet104”
将变为《portal200511sbet》
,
你能帮我实现这一点吗?
我有一个类似“portal100common2055”
的字符串。
我想把它分成两部分,第二部分应该只包含数字。
“portal200511sbet104”
将变为《portal200511sbet》
,
你能帮我实现这一点吗?
像这样:
Matcher m = Pattern.compile("^(.*?)(\d+)$").matcher(args[0]);
if( m.find() ) {
String prefix = m.group(1);
String digits = m.group(2);
System.out.println("Prefix is ""+prefix+""");
System.out.println("Trailing digits are ""+digits+""");
} else {
System.out.println("Does not match");
}
String[] parts = input.split("(?<=\D)(?=\d+$)");
if (parts.length < 2) throw new IllegalArgumentException("Input does not end with numbers: " + input);
String head = parts[0];
String numericTail = parts[1];
这个更优雅的解决方案使用regex的向后看和向前看
说明:
(?<=\D)
means at the current point, ensure the preceding characters ends with a non-digit (a non-digit is expressed as D
)(?=\d+$)
means t the current point, ensure that only digits are found to the end of the input (a digit is expressed as d
)这将只在您想要划分输入的所需点处为true
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...