0

I am creating a program which will reformat an XML file.

e.g.; The following is the XML snippet.

<ServiceRequest actionCode=KK
          addedWithoutItinerary=false airlineCode=1S
          code=ADTK orderNumber=1 serviceCount=1
          serviceType=SSR ssrType=AFX>

I want to surround all attribute values with double quotes.

e.g.;

<ServiceRequest actionCode="KK"
              addedWithoutItinerary="false" airlineCode="1S"
              code="ADTK" orderNumber="1" serviceCount="1"
              serviceType="SSR" ssrType="AFX">

Hence, I am trying to write a REGEX Pattern which can serve the purpose.

I want to extract all text strings between = and single whitespace character ("\s"), like "=false ", "=1S " and so on.

I tried this Pattern

private static final Pattern ATTR_PATTERN = Pattern.compile("(?<==)(.*)(?= |\\n)");

but this Pattern is grouping all texts which has ending character as whitespace.

e.g.;

KK
          addedWithoutItinerary=false airlineCode=1S
          code=ADTK orderNumber=1 serviceCount=1
          serviceType=SSR

Any suggestion?

sairajgemini
  • 353
  • 1
  • 3
  • 11

1 Answers1

2

You can search using this regex:

=([^\s"<>]+) 

And replace using ="$1"

RegEx Demo

Java Code:

String repl = xml.replaceAll("=([^\\s\"<>]+)", "=\"$1\"");

RegEx Details:

  • =: Match a =
  • ([^\s"<>]+): Match 1+ of any character that is not whitespace, ", < and >' and capture that in group #1
  • ="$1" is replacement part that puts = and capture value in double quotes.
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • But this will extract the exact word without = sign. Please suggest if I am wrong here. (?<==)([^\s"<>]+) Link: https://regexr.com/5go7f – sairajgemini Nov 21 '20 at 13:49
  • 1
    Yes a lookbehind will also work as you are suggesting – anubhava Nov 21 '20 at 14:52
  • Hi @anubhava can you please suggest how to read the value of $1? e.g.; if("$1".equals("some value")) { //Do something... } – sairajgemini Nov 24 '20 at 14:23
  • 1
    If you want value of `$1` separately then you will need to use `Pattern and Matcher` and then use `matcher.group(1)`. You can check sample Java code in the regex101 link I posted in my answer. – anubhava Nov 24 '20 at 15:10
  • 1
    Yes, that's the way I know. I was thinking is there a way to read the value directly from the metacharacter. Thanks for clarifying. – sairajgemini Nov 24 '20 at 15:15