1

I have three string lines here.


line 1 : 56,C0348|23064,C0319|23182,C0127|1476,C0378|2004,C0260|3306,C0091|23179,C0525|

line 2 : 8966,T4992|4446,T4992|4445,E|8965,E;T4992|8964,E;C0163|

line 3 : 920,M128;C0323|


Now, my goal is to remove strings that have a form of "Tnumber|","Cnumber|" , "Mnumber|".

For example, in the first line, i want to delete "C0348|", "C0319|", "C0127|", "C0378|", "C0260|", "C0091|", "C0525|

In the second line , T4992|, T4992, T4992|, C0163|

In the third line, M128; C0323|

How can i remove those strings from each string line?

I think i need to use regular expression, right? but, i still can not solve this problem.

Could you please help me?

6
  • What is the expected output? Commented Apr 30, 2014 at 6:38
  • ooppps sorry. the expected output will be "56,23064,23182,1476,2004,3306,23179, for the first line. Commented Apr 30, 2014 at 6:42
  • "8966,4446,4445,E|8965,E,;|8964,E;" for the second line. Commented Apr 30, 2014 at 6:43
  • 920, for the last line. Commented Apr 30, 2014 at 6:43
  • Rather go for a "proper" Java parser: Split the Strings on every separator (,) and then you can apply a regular expression. Commented Apr 30, 2014 at 6:44

3 Answers 3

2

You can use Regular expression, like this

import java.util.regex.Pattern;

public class Test {

    private static final String REGEX = "(T|M|C)\\d+(;|\\|)?";
    private static final Pattern p = Pattern.compile(REGEX);

    public static String filterer(String inputString) {
        String result = "";
        for(String s : p.split(inputString)) {
            result += s;
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(filterer("56,C0348|23064,C0319|23182,C0127|1476,C0378|2004,C0260|3306,C0091|23179,C0525|"));
        System.out.println(filterer("8966,T4992|4446,T4992|4445,E|8965,E;T4992|8964,E;C0163|"));
        System.out.println(filterer("920,M128;C0323|"));
    }
}

Output

56,23064,23182,1476,2004,3306,23179,
8966,4446,4445,E|8965,E;8964,E;
920,

RegEx Demo

Regular expression visualization

Debuggex Demo

Of course, instead of having capture groups, we can use character classes like this

Regular expression visualization

Debuggex Demo

Sign up to request clarification or add additional context in comments.

Comments

1

You may try a regexr of [TCM]\d+[\|;], http://regexr.com/v1?38ls3. And as java source:

String reg= "[TCM]\\d+[\\|;]";
String line = "56,C0348|23064,C0319|23182,C0127|1476,C0378|2004,C0260|3306,C0091|23179,C0525|";
line = line.replaceAll(reg, "");
System.out.println(line);
line = "8966,T4992|4446,T4992|4445,E|8965,E;T4992|8964,E;C0163|";
line = line.replaceAll(reg, "");
System.out.println(line);
line = "920,M128;C0323|";
line = line.replaceAll(reg, "");
System.out.println(line);

output:

56,23064,23182,1476,2004,3306,23179,
8966,4446,4445,E|8965,E;8964,E;
920,

3 Comments

@user3149929 In the third line, "M128;" should be deleted? But you said the rule is "Mnumber|", so the rule should be "Mnumber;"?
sorry, my mistake. So, expression would be [TCM]\\d+(;|\\|)?. right?
? means "Zero or one", so if "\" or ";" must appears once, it should be "[TCM]\\d+[\\|;]". I've edited my post.
0

as these are comma delimitered values you can split them as

String arr[] = line.split (",");

// iterate over them
int index = arr[0].indexOf ("|");
if (index != -1) {

   // do substring
} 

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.