Use this code numberOnly will contain your desired output.
String str="sdfvsdf68fsdfsf8999fsdf09";
String numberOnly= str.replaceAll("[^0-9]", "");
Answer from user1999257 on Stack OverflowUse this code numberOnly will contain your desired output.
String str="sdfvsdf68fsdfsf8999fsdf09";
String numberOnly= str.replaceAll("[^0-9]", "");
I always like using Guava String utils or similar for these kind of problems:
String theDigits = CharMatcher.inRange('0', '9').retainFrom("abc12 3def"); // 123
Extracting mixed number and first string from string [java]
Java Regular Expression, extract 5 digit numbers from a string
Extract number from a string
java - regex - Extract number from string. Number should be first 5 digit from my input - Stack Overflow
Say I have a string (ingAmntTemp) with a number followed by a unit of measure (ex. 1 1/2 cups). I need to split the number from the unit of measure and store both into separate variables (ingUnit and ingNum)... how would I do this?
The problem is that the number may be there, but not the unit. Also, the number may have a whole part but not a fractional part, or vice versa.
I know of regexes and I assume I need to use one here as an argument in .split() or .replaceAll(), but I have no idea how to go about doing it.
EDIT: Changed some mistakes
You may capture the value you need into a capturing group and access it with the corresponding code.
The regex for both Java and Python can look like
postgresql://[\d.]+:(\d+)\b
Details:
postgresql://- a literal stringpostgresql://[\d.]+- 1 or more digits or.symbols:- a colon(\d+)- Group 1 capturing 1 or more digits\b- a word boundary.
See the Java demo:
String s = "Install: C:\\Program Files\\app\nDatabase: postgresql://127.0.0.1:42018/app\nStarted: 2016-12-28 10:40:05.908000\nLines: 1000000\nVersion: 4.1\nPID: 1736";
Pattern pattern = Pattern.compile("postgresql://[\\d.]+:(\\d+)\\b");
Matcher matcher = pattern.matcher(s);
if (matcher.find()){
System.out.println(matcher.group(1));
}
See the Python code:
import re
s = "Install: C:\\Program Files\\app\nDatabase: postgresql://127.0.0.1:42018/app\nStarted: 2016-12-28 10:40:05.908000\nLines: 1000000\nVersion: 4.1\nPID: 1736";
pattern = r"postgresql://[\d.]+:(\d+)\b"
m = re.search(pattern, s)
if m:
print(m.group(1))
I'm not sure regex is your best option here, depending on the programming language, you may want to extract the port using some libraries.
If you still insist to use regex, you should make it more specific, for example:
postgresql:[^:]+:(\d+)
If your input string represents a Date then you could do
DateFormat df = new SimpleDateFormat("yyyyMMdd");
Date date = df.parse("19921231");
System.out.println(date.getMonth() + 1);
Or, you can also use the Calendar class as
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse("19921231"));
System.out.println(cal.get(Calendar.DAY_OF_MONTH)); // 31
System.out.println(cal.get(Calendar.YEAR)); // 1992
System.out.println(cal.get(Calendar.MONTH) + 1); // 12
Assuming that's a date format that will always be like that, why not use a subtring() method ( http://www.tutorialspoint.com/java/java_string_substring.htm )?
Or perhaps check this out as well, probably the best option once you do a little research into it: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html