0

For example : <element name="XYZ" type="string">9032905776</element>

In above regex we need to check whether the name attribute has value "XYZ" or not.

if name attribute contain "XYZ" then it should skip type="String" and mask 9032905776 as ***.

Expected Output: <element name="XYZ" type="string">***</element>

final String expressionTemplate = "(?<=<element name=\"(?i:XYZ)\"[a-z?]>)(.*?)(?=</element>)";
System.out.println("Regex :"+expressionTemplate);
System.out.println("\nRequest Before    :"+dummy);
System.out.println("Request After   :"+dummy.replaceAll(expressionTemplate.toString(), "*****"));

out put :

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
  • 2
    Off-topic, but why `expressionTemplate.toString()` when `expressionTemplate` is already a `String`? Also, [this post](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) might be an enlightening (and memorable) read. – hnefatl Apr 26 '18 at 17:05
  • 2
    Note that regex cannot parse HTML/XML and requires a parser to do this properly. You'll mangle the file in some cases if you do it this way. – markspace Apr 26 '18 at 17:06
  • See https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – rmlan Apr 26 '18 at 17:09
  • ignore that expressionTemplate.toString() – Namburi Manikanta Apr 26 '18 at 17:10
  • Are you certain that the `` tag is the only tag whicb needs the replacement and that no other tags would be nested inside it? – Tim Biegeleisen Apr 26 '18 at 17:15
  • input :458796 (?<=)(.*?)(?=) this expression is replacing and getting proper output is : ****** But, for this input : 8805450294 OUt put is same :::::::::::::8805450294 – Namburi Manikanta Apr 26 '18 at 17:26

1 Answers1

0

Don't Parse HTML With Regex

The problem is, no matter what regex we write, it would be easy to come up with a case where it doesn't work. Just imagine when special characters could be found inside type="string".

Instead you should use a real Java HTML parser (for example, jsoup)


However, if you want to disregard my warnings, and use a regex that could easily break on some unusual inputs, then try something like this:

final String expressionTemplate = "(?<=<element name=\"XYZ\"[^>]{0,99}>)(.*?)(?=</element>)";

Known issues:

  • breaks if > appears in type="string"
  • breaks if opening tag longer than specified amount (see hard-coded 99)
  • many other ways it could break... <!-- HTML comments, etc.
Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
  • String dummy1 = "<=asasa9032905776"; final String expressionTemplate = "(?<=)(.*?)(?=)"; System.out.println("Regex :" + expressionTemplate); System.out.println("\nRequest Before :" + dummy1); System.out.println("Request After :" + dummy1.replaceAll(expressionTemplate.toString(), "*****")); – Namburi Manikanta Apr 27 '18 at 08:01
  • 1
    @NamburiManikanta what is the purpose of this comment? Did you read my answer? – Patrick Parker Apr 27 '18 at 08:12