shahshi15
shahshi15

Reputation: 2987

Finding Pattern from one string to apply to another string - Java

So I have a string like this: <em>1234</em>56.70 it's basically a number where the em tags help identify what to highlight in the string

I need to first convert the string to an actual number with the current locale format. So I remove the em tags (replaceAll by emptyString) and then use the numberFormat java API to get a string like: $123,456.70

The problem with this is, I lost the highlight (em) tags. So I need to put it back in the string that is formatted, something like this: <em>$123,4</em>56.70

highlightValue = "<em>1234</em>56.70";
highlightValue = highlightValue.replaceAll("<em>", "").replaceAll("</em>", ""); // highlightValue is now 123456.70
highlightValue = numberFormat.convertToFormat(highlightValue, currencyCode); // highlightValue is now $123,456.70
highlightValue = someFunction(highlightValue); // this function needs to return <em>$123,4</em>56.70

I am not sure what approach to use. I was trying pattern matching but didn't know how to achieve it.

All help appreciated !

Upvotes: 2

Views: 98

Answers (3)

Japu_D_Cret
Japu_D_Cret

Reputation: 638

I took an approach, where I count the numbers in front of the tag, in the middle of the tag - as I think no formatting will actually change the numbers(assuming you don't add leading zeroes) and after that I insert back the tag based on the numbers which were in front of the tag or for the closing tag in front and inside

so this is the code:

public static void main(String[] args) {
  String input1 = "<em>1234</em>56.70";
  String result1 = formatString(input1, "em");
  System.out.printf("input1  = %s%n", input1);
  System.out.printf("result1 = %s%n", result1);

  String input2 = "<em>8127</em>29.12";
  String result2 = formatString(input2, "em");
  System.out.printf("input2  = %s%n", input2);
  System.out.printf("result2 = %s%n", result2);
}

private static String formatString(String input, String tagName) {
  String tagOpening = String.format("<%s>", tagName);
  int tagOpeningLength = tagOpening.length();
  String tagClosing = String.format("</%s>", tagName);
  int tagClosingLength = tagClosing.length();

  int inputLength = input.length();


  int tagOpeningPos = input.indexOf(tagOpening);
  int tagClosingPos = input.indexOf(tagClosing, tagOpeningPos);

  String beforeTag;
  if(tagOpeningPos > 0)
    beforeTag = input.substring(0, tagOpeningPos);
  else
    beforeTag = "";
  int digitsInBeforeTag = countNumbers(beforeTag);

  String tagValue;
  if(tagOpeningPos + tagOpeningLength < tagClosingPos)
    tagValue = input.substring(tagOpeningPos + tagOpeningLength, tagClosingPos);
  else 
    tagValue = "";
  int digitsInTagValue = countNumbers(tagValue);

  String afterTag;
  if((tagClosingPos + tagClosingLength) <  inputLength)
    afterTag = input.substring(tagClosingPos + tagClosingLength);
  else
    afterTag = "";

  String valueToBeFormatted = beforeTag + tagValue + afterTag;
  double value = Double.parseDouble(valueToBeFormatted);

  NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);

  String formattedValue = nf.format(value);

  int newEmOpeningPos = findSubstringWithThisManyNumbers(formattedValue, digitsInBeforeTag);
  int newEmClosingPos = findSubstringWithThisManyNumbers(formattedValue, digitsInBeforeTag+digitsInTagValue);

  StringBuilder result = new StringBuilder();

  result.append(formattedValue.substring(0, newEmOpeningPos));
  result.append(tagOpening);
  result.append(formattedValue.substring(newEmOpeningPos, newEmClosingPos));
  result.append(tagClosing);
  result.append(formattedValue.substring(newEmClosingPos));

  return result.toString();
}

private static int findSubstringWithThisManyNumbers(String input, int digitCount) {
  int pos = 0;

  int counter = 0;
  for(char c : input.toCharArray()) {
    if(counter >= digitCount)
      break;

    if(Character.isDigit(c))
      counter++;

    pos++;
  }

  return pos;
}

private static int countNumbers(String str) {
  int result = 0;
  for(char c : str.toCharArray())
    if(Character.isDigit(c))
      result++;
  return result;
}

the output was

input1  = <em>1234</em>56.70
result1 = <em>123,4</em>56.7
input2  = <em>8127</em>29.12
result2 = <em>812,7</em>29.12

Upvotes: 1

Sangbok  Lee
Sangbok Lee

Reputation: 2228

I don't know how can this be practical. But anyway.

    String highlightValue = "0<em>1234</em>56.70";
    int startIndex = highlightValue.indexOf("<em>");
    String startString = highlightValue.substring(0, startIndex);
    String endString = highlightValue.substring(highlightValue.indexOf("</em>") + "</em>".length());

    highlightValue = highlightValue.replaceAll("<em>", "").replaceAll("</em>", "");
    highlightValue = numberFormat.convertToFormat(highlightValue, currencyCode);
    // highlightValue is now $123,456.70

    int endIndex = highlightValue.indexOf(endString);
    highlightValue = startString + "<em>" + highlightValue.substring(0, endIndex) + "</em>" + endString;

    System.out.println(highlightValue);
    // 0<em>$123,4</em>56.70 

Upvotes: 0

Rajarshi basu
Rajarshi basu

Reputation: 352

I am assuming that you want to highlight the number from starting up to some number of digits.This can be done. In the initial string count the number of digits after which the tag is present. The starting tag will always be placed at the beginning. It is the ending tag you have to worry about. Now count the number of digits, excluding any other symbols.When the required number of digits have been passed, again place the tag. Either you can create a StringBuilder from the String highlighted and insert the tag string directly, or divide the string into two substrings and then join them together with the tag string in the middle.

Hope this helped.

Upvotes: 1

Related Questions