0

i' going to extract business value from a String. But my problem is, that the string repeates each time in a loop. And the content syntax is always the same, but the content is changing. Thus I need a reg-expr which helps me to extract the data.

What I have tried so far: ("^\\d{1,2}(.{1})\\s([A-Za-z]{2})\\s(([A-Z]\\d{2,3}))\\s.")

But the the above provided pattern outputs me only the first three arguments: 01. Di F929

Sample String shows like following:

01. Di F929 * Fr F929 Fr FREI Mo S688 Mi S49 * Sa S57 Mo F929 
Do F224 So S49 Di X337 Fr F56 So FREI \n

Let me explain how the string is build.

  1. (01.) is a digit from 01 to 31.
  2. Following be shortWeekDays (German) (Mo, Di, Mi, ..)
  3. Word start with Character ends with digit (F929, S49, ...)
  4. (Optional) a special character like '*, X, ...'

Important:

Di + F929 + *
represents single data-block. Each String contains about 12 data-blocks.

My need is, a regular expression which match the above problem. Thanks in regard!

10
  • 1
    Um...can you tell us what you are trying to match in that sample string? Commented Dec 25, 2018 at 13:43
  • Try removing the ^ anchor Commented Dec 25, 2018 at 13:48
  • Removing the anchor ^ doesn't help me. The word 'FREI' in the string doesn't appear or doesn't match Commented Dec 25, 2018 at 13:51
  • It's really unclear what you want to achieve. Please show some test cases, with input string and expected output. Commented Dec 25, 2018 at 13:56
  • Ok, i have solved it. Commented Dec 25, 2018 at 13:58

1 Answer 1

1

This will help you:

(\d{1,2}\.)?\s*([A-Za-z]{2}\s+[A-Z0-9]+\s*[*X]?)\s*

Online Demo

Java code:

public class Test {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("(\\d{1,2}\\.)?\\s*([A-Za-z]{2}\\s+[A-Z0-9]+\\s*[*X]?)\\s*");
        String string = "01. Di F929 * Fr F929 Fr FREI Mo S688 Mi S49 * Sa S57 Mo F929 " +
            "Do F224 So S49 Di X337 Fr F56 So FREI \\n";

        Matcher m = pattern.matcher(string);

        while (m.find())
            System.out.println(m.group(2));
    }
}
Sign up to request clarification or add additional context in comments.

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.