Betafish
Betafish

Reputation: 1262

Java Pattern for regex

I have a 2 column tsv file with column one having

1-FN3Z1-206329557431
1-FN411-153115736976

Where I am trying to remove the first two parts of the value (i.e to extract 206329557431 and 153115736976). I've used online regex tool to generate the patterns

pattern

".*?\\d+.*?\\d+.*?(\\d+)" AND ".*?\\d+.*?\\d+.*?\\d+.*?(\\d+)"

Independently they work fine. I'm trying to look for a combined regex pattern. Any pointers as to how this can be done.

Upvotes: 1

Views: 81

Answers (3)

Andriy Rymar
Andriy Rymar

Reputation: 313

How about this regexp : .-.{5}- looks like it can matches all statements but it depends on your format.Example of pattern work

Here is Java code example :

@Test
public void test() {
    String test = "1-FN3Z1-206329557431    1-FN411-153115736976";

    String result = test.replaceAll(".-.{5}-", "");

    assertEquals("206329557431    153115736976", result);
}

Upvotes: 1

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

Why don't use split for example :

String spl = "1-FN411-153115736976".split("-")[2];

If you want a regex you can use (.*?-){2}(.*), which mean get everything after the second -

regex demo

Output

206329557431
153115736976

Upvotes: 4

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

If the strings in your TSV file all have the same widths and patterns, then you can just use substring here:

String tsv = "1-FN3Z1-206329557431";
System.out.println(tsv.substring(8));

Demo

Upvotes: 1

Related Questions