Forum Stats

  • 3,816,120 Users
  • 2,259,147 Discussions
  • 7,893,392 Comments

Discussions

Regular Expressions

Aykut
Aykut Member Posts: 53
edited Aug 31, 2016 3:28AM in Swing

Hello!

Say I have a csv file with following content:

1;2;3;4;5;6

a;b;c;d;e;s

p;q;r::t;d

q:w:e:r:t;p

l;k;h;;f;w

;z;k;p;;m

i.e. 6 values each line; In excel it would be 6 columns. (note: sometimes values are missing, such 1st value of last line)

I want a Regular Expressions to delete certain columns, such 2 and 5

that means the 2nd and the 5th column should be deleted,

so the expected outcome is

1;3;4;6

a;c;d;s

p;r::d

q:e:r:p

l;h;;w

;k;p;m

How can I do such deleting without looping the lines? Is this possible?

String value = getValueFromCSV();  // this line to read a csv is no problem, I have this function

But what do I need to apply on "value" via RegEx :-)

Thanks,

Regards,
Aykut

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Aug 30, 2016 4:34AM
    import static org.junit.Assert.assertEquals;import static org.junit.Assert.fail;import java.util.Arrays;import java.util.Collection;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.Parameterized;import org.junit.runners.Parameterized.Parameters;@RunWith(value = Parameterized.class)public class CsvColumnRemoverTest {    private static final String FILTER_PATTERN = "([^:;]*[:;])[^:;]*[:;](([^:;]*[:;]){2})[^:;]*[:;](.*)";    private String _input;    private String _expected;    @Parameters    public static Collection<Object[]> data() {        return Arrays.asList(new Object[][] {            {"1;2;3;4;5;6","1;3;4;6"},            {"a;b;c;d;e;s","a;c;d;s"},            {"p;q;r::t;d","p;r::d"},            {"q:w:e:r:t;p","q:e:r:p"},            {"l;k;h;;f;w","l;h;;w"},            {";z;k;p;;m",";k;p;m"},        });    }    public CsvColumnRemoverTest(String input, String expected) {        _input = input;        _expected = expected;    }    @Test    public void test() {        Pattern filterPattern = Pattern.compile(FILTER_PATTERN);        Matcher filterMatcher = filterPattern.matcher(_input);        if (!filterMatcher.find()) {            fail(String.format("pattern '%s' does not match data: %s", FILTER_PATTERN, _input));        }        String result = String.format("%s%s%s", filterMatcher.group(1), filterMatcher.group(2), filterMatcher.group(4));        assertEquals(_input, _expected, result);    }}

    bye

    TPD

  • Aykut
    Aykut Member Posts: 53
    edited Aug 30, 2016 3:16PM

    Dear TPD,

    thank for your answer. I think you got my point.

    I am not a java expert. I can not reuse the snippet you provided. :-(

    On internet I got a similar solution as your approach.

    Please have a look in the following code:

    - case1 with single line works fine!

    - case2 with multiple lines does not work

    So what do I need to add as RegEx for "all lines"

    Currently it applies only on the last line.

    package main;public class CsvColumnRemoverTest {          public static void main(String[] args) {    String case1 = "A;B;C;D;E";       String case2 = "A;B;C;D;E" + "\n";    case2 += "A;B;C;D;E" + "\n";        case2 += "A;B;C;D;E";       // ------------------------------------------------------------------------------    System.out.println("#Before");    System.out.println(case1);    System.out.println();    System.out.println(case2);       // replacement    case1 = case1.replaceAll("([^<]*);([^<]*);([^<]*);([^<]*);([^<]*)", "$1;$3;$4;$5");    case2 = case2.replaceAll("([^<]*);([^<]*);([^<]*);([^<]*);([^<]*)", "$1;$3;$4;$5");  // <= what do I need to add here, so that all lines are processed       // ------------------------------------------------------------------------------    System.out.println();    System.out.println("#After");      System.out.println(case1);    System.out.println();    System.out.println(case2);    }}  

    Outcome of the code above:

    #BeforeA;B;C;D;EA;B;C;D;EA;B;C;D;EA;B;C;D;E#AfterA;C;D;EA;B;C;D;EA;B;C;D;EA;C;D;E

    Thanks,
    Aykut

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Aug 31, 2016 3:28AM
    Aykut wrote:Dear TPD,thank for your answer. I think you got my point.I am not a java expert. I can not reuse the snippet you provided. :-(

    The relevant lines in my SSCCE are 17, 40-43 and 44.

    Along with the API of Pattern (Java Platform SE 8 ) and  Matcher (Java Platform SE 8 ) you should be able to solve your problem.

    bye

    TPD

This discussion has been closed.