0

This is a continuation of a question I asked earlier. I need to extract a date pattern, which is surrounded by the strings String1, String2, String3 String4. What I did was

Pattern pattern = Pattern.compile("(?<=String1\sString2\s(?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\\1(?:19|20)?\\d\\d?=\sString3\sString4)");

my date pattern is

(0?[1-9]|[12][0-9]|3[01])([- /.])(0?[1-9]|1[012])\\2(19|20)\\d\\d

which works fine but when trying to surround it with strings, I am facing trouble.

The date is in between String2 and String3. I am quite sure there is something wrong, as there is an error on my program saying invalid escape sequence but I can't figure it out. Any help is appreciated. Thanks in advance.

3 Answers 3

3

Here you have an invalid escape sequence:

"...(?<=String1\sString..."
               ^^

You have to escape the backslash literal in a java String to pass it into a regular expression pattern:

"...(?<=String1\\sString..."
               ^^^

You already had it right with the \\d for digits, but not with the \\s for whitespace.

Sign up to request clarification or add additional context in comments.

10 Comments

Is that the only flaw? Ok will try now.
+1 \s is not a valid java escape sequence. You are coding a java String literal, so you need the double backslash \\s to code the regex \s
Ok I corrected that, but it still does not seem to work. Program generates errors when run. Can someone show me how to extract the date pattern, along with the strings. The strings are specific so no pattern required.
@newtoprogramming: post the exact error messages. Also, don't only describe the input, also show some real input and the expected output, and we can help you better. You can always use the edit button below your question.
@jlordo The expected output, should be String1 String2 7/2/2013 String3 String4, which needs to be extracted and copied in an arraylist. The regex, must be matched with an input string, which contains more than a 100 words along with the line of the pattern.
|
2

Your regex is ending with:

\\d?=\sString3\sString4)

There it looks like you missed an opening square bracket to make it positive lookahead and of course \s should be \\s. Change that part to:

\\d(?=\\sString3\\sString4)

5 Comments

Yes I have changed that. But I do not understand. Should I enclose the whole Regex in Square brackets?
@newtoprogramming: Why do you want do that? You should now take the time to understand how your pattern works.
No square brackets are for character class. If you explain your problem clearly we can try to help you.
@anubhava this is the exact line for which I am trying to write a regex "commented on 7/2/2013 (version1.0)" for which, I tried "commented on (?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\\1(?:19|20)?\\d\\d (version 1.0)" and also "(?<=commented\\son\\s(?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\\1(?:19|20)?\\d\\d?=\\s(version 1.0))" neither detects my pattern. Can you help me. I do not know what I am doing wrong.
Can you please provide this code in your question as it is very difficult to read from comment.
1

I know that many people are not aware of the features of the wonderful class MessageFormat, so here a quick reminder:

MessageFormat format = new MessageFormat("String1 String2 {0,date} String3 String4");
try {
    Object[] parse = format.parse("String1 String2 31.8.2000 String3 String4");
    Date date = (Date) parse[0];
    System.out.println(date);
} catch (ParseException e) {
    e.printStackTrace();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.