?
Solved

How to abstract a sub-string fro ma string by using Regular Expression?

Posted on 2009-02-20
10
Medium Priority
?
750 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:chenwei
  • 6
  • 3
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 23690659
Try the following:
s = s.replaceAll(".*\\d{2]\\.\\d{2}\\.\\d{4}.*", "$1");

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23690665
Oops sorry
  	
 
s = s.replaceAll(".*(\\d{2]\\.\\d{2}\\.\\d{4}).*", "$1");

Open in new window

0
 
LVL 10

Expert Comment

by:mahome
ID: 23690672
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));

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 23690696
Of course, if you want to retain the original:
String date = s.replaceAll(".*(\\d{2]\\.\\d{2}\\.\\d{4}).*", "$1");

Open in new window

0
 

Author Comment

by:chenwei
ID: 23690697
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.PatternSyntaxException: 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.IndexOutOfBoundsException: No group 1
      at java.util.regex.Matcher.group(Matcher.java:463)
      at java.util.regex.Matcher.appendReplacement(Matcher.java:730)
      at java.util.regex.Matcher.replaceAll(Matcher.java:806)
      at java.lang.String.replaceAll(String.java:2000)
      at com.weichen.app.alltest.FilterSubString.main(FilterSubString.java:11)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23690723
See my correction
0
 

Author Comment

by:chenwei
ID: 23690762
I think the correct expression should be:
replaceAll(".*(\\d{2}\\.\\d{2}\\.\\d{4}).*", "$1");

The first ']' should be '}'. :-)

Many thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23690768
So sorry chenwei - there was still a typo in there, so
  	
 
String date = s.replaceAll(".*(\\d{2}\\.\\d{2}\\.\\d{4}).*", "$1");

Open in new window

0
 

Author Closing Comment

by:chenwei
ID: 31549176
Simply wonderful.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23690809
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question