0

When i create a pattern - Pattern.compile("+:") to extract the info of the following line

Pattern pattern = Pattern.compile("+:");
String fields[] = pattern.split( "UNB+UNOA:1+KRR+KRR+050313:1257+1+++++KRR");

the line Pattern.compile("+:"); shows dangling meta-character error.

How to fix it?

Thanks.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Kathir J
  • 1
  • 3
  • Possible duplicate of [Java - Escaping Meta-characters \[ and \] in Regex](http://stackoverflow.com/questions/7904695/java-escaping-meta-characters-and-in-regex) – Julien Lopez May 26 '16 at 08:38

2 Answers2

1

You should escape the metacharacter with a backslash. As the backslash is itself needs escaping in Java, then you need to repeat it:

Pattern pattern = Pattern.compile("\\+:");
ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • This works absolutely fine..also, i would like to know how to get and process line by line similar for each for the following.....can u please help me Stream steamList = Files.lines(Paths.get("sample.txt")) .map(line -> line.split("'")) // Stream .flatMap(Arrays::stream) // Stream .distinct(); for(Stream line: steamList) { String line = ""; String empName = line.subString()... } – Kathir J May 26 '16 at 08:44
  • Sorry..the solution doesn't work and prints me the entire string instead of splitting up – Kathir J May 26 '16 at 08:52
  • public void process(String line) { System.out.println("Line is: "+line); Pattern pattern = Pattern.compile("\\+:"); String fields[] = pattern.split( line.toString() ); for(String field: fields) { System.out.println("Field: "+field); } System.exit(1); } – Kathir J May 26 '16 at 08:52
  • Input: UNB+UNOA:1+KRR+KRR+050313:1257+1+++++KRR output: UNB+UNOA:1+KRR+KRR+050313:1257+1+++++KRR – Kathir J May 26 '16 at 08:53
  • i am getting the same result in online as well - http://www.regexplanet.com/advanced/java/index.html – Kathir J May 26 '16 at 08:54
  • @KathirJ: If your intention is to split either by colon or plus sign, then the pattern you should use is "(\\+)|(:)". This pattern means "plus sign or colon", while the one in the answer means "plus sign followed by a colon". If you update your question, I'll update the answer. – ernest_k May 26 '16 at 08:57
  • Intention is to get values before plus or first/second/third value based on the need. for example when i say just need the first value...whether the text contains colon or without colon it should return the value. if it has multiple values. i should be able to get first/second/third value. can you please help me with a pattern? – Kathir J May 26 '16 at 09:06
  • What do you consider to be the delimiter for your values: 1. plus, 2. colon, 3. plus followed by colon, 4. plus, colon, or plus followed by colon? It's unclear what the delimiter is. Maybe you should mention the expected result for the string in your example? – ernest_k May 26 '16 at 09:14
  • Input: UNB+UNOA:1+KRR+KRR+050313:1257+1+++++KRR' Expected: Field[0] = UNB Field[1][2] = 1 Field[2] = KRR Field[4][1]=050313 the above is just an example. basically faster way to get each values whether it comes with plus or comes with : – Kathir J May 26 '16 at 09:36
  • This means that you expect a 2-dimensional array, with the first dimension being split on the plus sign, and the second one being split on the colon. If this is the case, then you cannot achieve the split with a single call to String.split(). You would need to do it twice, and that would also allow you to organize the logic of your code. So, you'll first split on "\\+", then for each element of the obtained array (you loop over them), you split on ":". – ernest_k May 26 '16 at 09:43
0

You can use the quoted version too:

    String quotedPlus = Pattern.quote("+");
    Pattern pattern = Pattern.compile(quotedPlus + ":");
    String str = "3+:";
    System.out.println(pattern.matcher(str).find()); // true

You can see more on that on this SO question.

Community
  • 1
  • 1
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49