-5

data: <emp_dist_nm>Shyam lal/tester</emp_dist_nm>

code i tried is

Pattern p3 = Pattern.compile("<emp_dist_nm>(\\S+)</emp_dist_nm>");

The pattern is not getting compile, kindly suggest the exact symbol i need to use so that my pattern can compile

tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • data: Shyam lal/tester code i tried is Pattern p3 = Pattern.compile("(\\S+)"); The pattern is not getting compile, kindly suggest the exact symbol i need to use so that my pattern can compile – Sambeet Das Mar 14 '19 at 11:48
  • 1
    I suggest using a regex site like regex101.com to check your regex -> https://regex101.com/r/mhCWSz/1 – Lino Mar 14 '19 at 11:50
  • Well, you are looking for `\S+`, but there is a `\s` in the name. – tobias_k Mar 14 '19 at 11:50
  • @tobias_k I suppose your edit is very confusing, as you fixed the escaping error on the fly. – Gyro Gearless Mar 14 '19 at 11:51
  • 2
    @GyroGearless No, I did not fix any errors, I just marked it as code. The missing \ was just how SO rendered the non-code-regex – tobias_k Mar 14 '19 at 11:52
  • @GyroGearless see the side-by-side markdown of the edit, you see that tobias_k just edited the markdown to render correctly – Lino Mar 14 '19 at 11:54
  • So then the pattern compiles fine, @SambeetDas what is the exact error message? – Gyro Gearless Mar 14 '19 at 11:54
  • Don't parse XML with a regex https://stackoverflow.com/questions/8577060/why-is-it-such-a-bad-idea-to-parse-xml-with-regex – keuleJ Mar 14 '19 at 12:20

1 Answers1

3

The pattern does compile, it just does not match the string. You are looking for \S+, but that does not match the space in the name. Instead, you could, e.g., try "everything except <", i.e. [^<]+

Pattern p3 = Pattern.compile("<emp_dist_nm>([^<]+)</emp_dist_nm>");

In fact, since you already have the closing tag after the group you want to capture, you could also just use .+?, i.e. a non-greedy group of any character. Here, the non-greedy ? is important, otherwise it might merge the contents of two such tags and everything in between.

Pattern p3 = Pattern.compile("<emp_dist_nm>(.+?)</emp_dist_nm>");

If you also want to allow empty tags, use * instead of +, i.e. [^<]* or .*?.

tobias_k
  • 81,265
  • 12
  • 120
  • 179