chenwei
asked on
How to abstract a sub-string fro ma string by using Regular Expression?
Assumed I have a string as follow:
"ÖÄÖÜ Stand:¶R **üü 12.02.2009ÿÂ"
What I want is, I want to abstract the date "12.02.2009" from this string. I guess one can do this in a simple way by using Regular-Expression.
Has someone idea?
"ÖÄÖÜ Stand:¶R **üü 12.02.2009ÿÂ"
What I want is, I want to abstract the date "12.02.2009" from this string. I guess one can do this in a simple way by using Regular-Expression.
Has someone idea?
Oops sorry
s = s.replaceAll(".*(\\d{2]\\.\\d{2}\\.\\d{4}).*", "$1");
Yes regex is what you need
String input = "ÖÄÖÜ Stand:¶R **üü 12.02.2009ÿÂ";
Pattern pattern = Pattern.compile(".*([0-9]{2}\\.[0-9]{2}\\.[0-9]{4}).*");
Matcher matcher = pattern.matcher(input);
matcher.find();
System.out.println(matcher.group(1));
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I do as follow:
private static final String STAND = "ÖÄÖÜ Stand:¶R **üü 12.02.2009ÿÂ";
/**
* @param args
*/
public static void main(String[] args) {
String subStr = STAND.replaceAll(".*\\d{2] \\.\\d{2}\ \.\\d{4}.* ", "$1");
System.out.println(subStr) ;
}
But I get exception as follow:
Exception in thread "main" java.util.regex.PatternSyn taxExcepti on: Unclosed counted closure near index 6
.*\d{2]\.\d{2}\.\d{4}.*
^
So I correct your regular-expression as follow:
".*\\d{2}\\.\\d{2}\\.\\d{4 }.*", "$1"
But I get exception as follow:
Exception in thread "main" java.lang.IndexOutOfBounds Exception: No group 1
at java.util.regex.Matcher.gr oup(Matche r.java:463 )
at java.util.regex.Matcher.ap pendReplac ement(Matc her.java:7 30)
at java.util.regex.Matcher.re placeAll(M atcher.jav a:806)
at java.lang.String.replaceAl l(String.j ava:2000)
at com.weichen.app.alltest.Fi lterSubStr ing.main(F ilterSubSt ring.java: 11)
private static final String STAND = "ÖÄÖÜ Stand:¶R **üü 12.02.2009ÿÂ";
/**
* @param args
*/
public static void main(String[] args) {
String subStr = STAND.replaceAll(".*\\d{2]
System.out.println(subStr)
}
But I get exception as follow:
Exception in thread "main" java.util.regex.PatternSyn
.*\d{2]\.\d{2}\.\d{4}.*
^
So I correct your regular-expression as follow:
".*\\d{2}\\.\\d{2}\\.\\d{4
But I get exception as follow:
Exception in thread "main" java.lang.IndexOutOfBounds
at java.util.regex.Matcher.gr
at java.util.regex.Matcher.ap
at java.util.regex.Matcher.re
at java.lang.String.replaceAl
at com.weichen.app.alltest.Fi
See my correction
ASKER
I think the correct expression should be:
replaceAll(".*(\\d{2}\\.\\ d{2}\\.\\d {4}).*", "$1");
The first ']' should be '}'. :-)
Many thanks!
replaceAll(".*(\\d{2}\\.\\
The first ']' should be '}'. :-)
Many thanks!
So sorry chenwei - there was still a typo in there, so
String date = s.replaceAll(".*(\\d{2}\\.\\d{2}\\.\\d{4}).*", "$1");
ASKER
Simply wonderful.
:-)
Open in new window