2

I need to find and print out a particular word in a String. What regex can you recommend me to find a "9.1.1_offline" in following String:

EGA_SAMPLE_APP-iOS-master-<Any word>-200710140849862

Another examples are:

  • EGA_SAMPLE_APP-iOS-master-9.2.3_online-200710140849862
  • EGA_SAMPLE_APP-iOS-master-10.2.3_offline-200710140849862
Johannes N
  • 265
  • 3
  • 17
  • 2
    See [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236). Also, [Do we need a close reason for zero-effort questions?](http://meta.stackoverflow.com/questions/260828/do-we-need-a-close-reason-for-zero-effort-questions) – Wiktor Stribiżew Jul 12 '20 at 12:51

2 Answers2

0

I can suggest the following one line option using String#replaceAll:

String input = "EGA_SAMPLE_APP-iOS-master-9.2.3_online-200710140849862";
String target = input.replaceAll(".*\\b(\\d+\\.\\d+\\.\\d+_(?:online|offline))\\b.*", "$1");
System.out.println(target);

This prints:

9.2.3_online
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    @Toto The thing is, Wiktor is _right_ in that his duplicate link could serve as a good starting point for an answer here. But, I don't consider the duplicate specific enough to be an exact copy of this question. Maybe someone will open a discussion on the Meta site about this issue. – Tim Biegeleisen Jul 12 '20 at 12:50
  • You're right, but I'm not so fluent in English do discuss on Meta ;( – Toto Jul 12 '20 at 12:52
  • 2
    Also, imho, downvotijg an helpfull answer is never chic. Although a personal choice, I'd always value an answer separate from the question. People have different opinions though. Something covered in multiple threads on meta. – JvdV Jul 12 '20 at 14:09
0

Use the regex, \\d+\\.\\d+\\.\\d+\\_(offline|online)

Demo:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        // Test strings
        String[] arr = { "EGA_SAMPLE_APP-iOS-master-9.1.1_offline-200710140849862",
                "EGA_SAMPLE_APP-iOS-master-9.2.3_online-200710140849862",
                "EGA_SAMPLE_APP-iOS-master-10.2.3_offline-200710140849862" };
        Pattern pattern = Pattern.compile("\\d+\\.\\d+\\.\\d+\\_(offline|online)");

        // Print the matching string
        for (String s : arr) {
            Matcher matcher = pattern.matcher(s);
            while (matcher.find()) {
                System.out.println(matcher.group());
            }
        }
    }
}

Output:

9.1.1_offline
9.2.3_online
10.2.3_offline

Explanation of the regex:

  1. \\d+ specifies one or more digits
  2. \\. specifies a .
  3. \\_ specifies a _
  4. (offline|online) specifies offline or online.

[Update]

Based on the edited question i.e. find anything between EGA_SAMPLE_APP-iOS-master- and -An_integer_number: Use the regex, EGA_SAMPLE_APP-iOS-master-(.*)-\\d+

Demo:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        // Test strings
        String[] arr = { "EGA_SAMPLE_APP-iOS-master-9.1.1_offline-200710140849862",
                "EGA_SAMPLE_APP-iOS-master-9.2.3_online-200710140849862",
                "EGA_SAMPLE_APP-iOS-master-10.2.3_offline-200710140849862",
                "EGA_SAMPLE_APP-iOS-master-anything here-200710140849862" };

        // Define regex pattern
        Pattern pattern = Pattern.compile("EGA_SAMPLE_APP-iOS-master-(.*)-\\d+");

        // Print the matching string
        for (String s : arr) {
            Matcher matcher = pattern.matcher(s);
            while (matcher.find()) {
                System.out.println(matcher.group(1));
            }
        }
    }
}

Output:

9.1.1_offline
9.2.3_online
10.2.3_offline
anything here

Explanation of the regex:

.* specifies anything and the parenthesis around it specifies a capturing group which I've captured with group(1) in the code.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110