1

I am trying to write a regular expression to find all characters which are both preceded and followed by '+' sign.

Eg: For input

+z+45+v89+b+

I am expecting two groups i.e +z+ and +b+.

I have written following regular expression in java which works for the above input

\\+[a-zA-Z]\\+

but fails for following input

+a+b+c+

For second input, I am expecting following groups +a+ , +b+ and +c+

Bagira
  • 2,149
  • 4
  • 26
  • 55
  • 2
    try thus [`String text = "+a+b+c+"; String regex = "(?=(\\+[a-zA-Z]\\+))"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(text); while (matcher.find()) { System.out.println(matcher.group(1)); }`](https://ideone.com/1VlQvv) – Youcef LAIDANI Jan 10 '18 at 10:46
  • 1
    Thanks it works. – Bagira Jan 10 '18 at 11:05
  • Just wondering why we need to capture first group as in matcher.group(1)? If i use matcher.group(), i don't get desired result. – Bagira Jan 10 '18 at 12:43
  • because group(1) is represented by `(\\+[a-zA-Z]\\+)` and you have to capture that group, hope you get me? – Youcef LAIDANI Jan 10 '18 at 12:45
  • 1
    Yes, got your point. – Bagira Jan 10 '18 at 14:22

0 Answers0