0

I want to use regex in some part of string in java,

In below string, only emp-id-<dynamic empID> and project remain same for all the string.

Case1:  project/emp-id1545/ID-JHKDKHENNDHJSJ

Case 2: project/**dep**/emp-id8545/ID-GHFRDEEDE

I’ve scenario sometime string come with dep, temp, or no value like Case 1 after project.

How to filter only emp-id-<dynamic empID> from the above string, to work with case 1 and case 2?

5
  • What have you attempted? Please show your code. Commented Jul 17, 2015 at 18:10
  • Is CaseX also part of your data? Does your data contain only one line or many lines? Commented Jul 17, 2015 at 18:11
  • Nope! Only the project/emp-id1545/ID-JHKDKHENNDHJSJ and project/dep/emp-id8545/ID-GHFRDEEDE Commented Jul 17, 2015 at 18:17
  • Is project/emp-id1545/ID-JHKDKHENNDHJSJ project/dep/emp-id8545/ID-GHFRDEEDE one string or two strings? I am asking since I don't know if I can use ^project since ^ by default it will represent start of string, not start of line, so if there is some project/emp-id... at start of second line ^ will prevent us from finding it. Commented Jul 17, 2015 at 18:45
  • Thanks! its two different string, i mentioned two case to explain i may get the string contains with project/ and without project/ Commented Jul 17, 2015 at 19:38

1 Answer 1

1

There are a variety of ways you can accomplish this task

Regex

The pattern

"emp-id\\d+"

should achieve what you're wanting for both cases. The pattern matches "emp-id" plus 1 or more digits (\\d+).

public static void main(String[] args) throws Exception {
    String case1 = "project/emp-id1545/ID-JHKDKHENNDHJSJ";
    String case2 = "project/**dep**/emp-id8545/ID-GHFRDEEDE";

    Matcher matcher = Pattern.compile("emp-id\\d+").matcher(case1);
    // Changed from while to if cause we're only going to get the first match
    if (matcher.find()) {
        System.out.println(matcher.group());
    }

    matcher = Pattern.compile("emp-id\\d+").matcher(case2);
    // Changed from while to if cause we're only going to get the first match
    if (matcher.find()) {
        System.out.println(matcher.group());
    }
}

Results:

emp-id1545
emp-id8545

Java 8

Given that your data indicates that the character "/" is a delimiter. You can also use String.split() and Stream.filter() (Java 8) to find your String.

public static void main(String[] args) throws Exception {
    String case1 = "project/emp-id1545/ID-JHKDKHENNDHJSJ";
    String case2 = "project/**dep**/emp-id8545/ID-GHFRDEEDE";

    System.out.println(Arrays.stream(case1.split("/")).filter(s -> s.startsWith("emp-id")).findFirst().get());
    System.out.println(Arrays.stream(case2.split("/")).filter(s -> s.startsWith("emp-id")).findFirst().get());
}

Results:

emp-id1545
emp-id8545

Non Regex or Java 8

Still using "/" delimiter and "emp-id" you can use String.indexOf() and String.substring() to extract the String you're looking for.

public static void main(String[] args) throws Exception {
    String case1 = "project/emp-id1545/ID-JHKDKHENNDHJSJ";
    String case2 = "project/**dep**/emp-id8545/ID-GHFRDEEDE";

    int index = case1.indexOf("emp-id");
    System.out.println(case1.substring(index, case1.indexOf("/", index)));

    index = case2.indexOf("emp-id");
    System.out.println(case2.substring(index, case2.indexOf("/", index)));
}

Results:

emp-id1545
emp-id8545
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.