0

This is my data and its pattern:

// _23.02_ANTALYA____________FRANKFURT___________DE_7461_18:20-21:00________________
public static final String FLIGHT_DEFAULT_PATTERN = "\\s+\\d{2}.\\d{2}\\s[A-Z]+\\s+[A-Z]+\\s+[A-Z\\s]{3}[\\d\\s]{5}\\d{2}:\\d{2}-\\d{2}:\\d{2}\\s+";

Underscores are space character. Now I need a class that divides every regex term to data. For example

\\s+ = " "
\\d{2} = "23"
. = "."
\\d{2} = "02"
\\s = " "
[A-Z]+ = "ANTALYA"

etc... That must be ordered by pattern.

How can I do this or is there a library for this?

2
  • 6
    Read the docs. You need to capture groups. Commented Jan 27, 2014 at 12:28
  • Actually I imagine an algorithm for this but if there is a tool that can be good. I don't want waste my time. Commented Jan 27, 2014 at 12:42

2 Answers 2

2

As @devnull mentioned, you should use capturing groups:

(\s+)(\d{2})(.)(\d{2})(\s)([A-Z]+)(\s+)([A-Z]+)(\s+)([A-Z\s]{3})([\d\s]{5})(\d{2}:\d{2})(-)(\d{2}:\d{2})(\s+)

See the full explanation of this regular expression on Regex101.

You would then use something like the following to match the text and extract the individual values:

String text = " 23.02 ANTALYA            FRANKFURT            DE 7461 18:20-21:00                 ";
Pattern pattern = Pattern.compile("(\\s+)(\\d{2})(.)(\\d{2})(\\s)([A-Z]+)(\\s+)([A-Z]+)(\\s+)([A-Z\\s]{3})([\\d\\s]{5})(\\d{2}:\\d{2})(-)(\\d{2}:\\d{2})(\\s+)");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
    for (int i = 1; i < matcher.groupCount(); i++) {
        System.out.println(matcher.group(i));
    }
}

To make it easier to extract specific fields, you could (in Java 7 and later) use named capturing groups:

(?<LeadSpace>\s+)(?<Day>\d{2})(.)(?<Month>\d{2})...

You could then use something like the following to get each named group:

...
if (matcher.find()) {
    System.out.println(matcher.group("LeadSpace"));
    System.out.println(matcher.group("Day"));
    System.out.println(matcher.group("Month"));
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thx for your answer. I solved my problem with different way but your answer has more usable informations. And we're using java6 in company.
0

I found a different way. I divided pieces with my hand.

// _24.02_MAURITIUS_________HAMBURG________________via:FRA_DE/LH____08:30-20:05_____
public static final List<String> FLIGHT_VIA_PATTERN = Arrays.asList( "\\s+", "\\d{2}", "\\.", "\\d{2}", "\\s+", "[A-Z]+", "\\s+", "[A-Z]+", "\\s+", "via:", "[A-Z\\s]{4}", "[A-Z]{2,3}", "/",
        "[A-Z]{2,3}", "\\s+", "\\d{2}", ":", "\\d{2}", "\\-", "\\d{2}", ":", "\\d{2}", "\\s+" );

After this I used a loop and everything is fine. This question can close.

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.