【问题标题】:Finding Pattern from one string to apply to another string - Java从一个字符串中查找模式以应用于另一个字符串 - Java
【发布时间】:2017-03-28 05:36:55
【问题描述】:

所以我有一个这样的字符串:<em>1234</em>56.70 它基本上是一个数字,em 标签有助于识别字符串中要突出显示的内容

我需要先将字符串转换为具有当前语言环境格式的实际数字。所以我删除了em 标记(用emptyString 替换All),然后使用numberFormat java API 得到一个字符串,如:$123,456.70

问题在于,我丢失了高亮 (em) 标签。所以我需要把它放回格式化的字符串中,像这样:<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

我不确定使用什么方法。我正在尝试模式匹配,但不知道如何实现它。

感谢所有帮助!

【问题讨论】:

  • 输入字符串是否总是以&lt;em&gt;开头?

标签: java regex string format replaceall


【解决方案1】:

我假设您想突出显示从开始到某个位数的数字。这可以做到。 在初始字符串中计算出现标签的位数。起始标签将始终放置在开头。这是您必须担心的结束标签。现在计算位数,不包括任何其他符号。当通过所需的位数时,再次放置标签。要么你可以从String highlighted创建一个StringBuilder,然后直接插入标签字符串,或者将字符串分成两个子字符串,然后将它们与中间的标签字符串连接在一起。

希望这会有所帮助。

【讨论】:

  • 我假设您想突出显示从开始到某个数字的数字。这是一个大胆的假设。
  • 如果假设不成立,同样的想法可以应用于开始和结束标签。我会把它留给提问者。但是,如果原始字符串的前导零不能进入格式化字符串,这是需要考虑的另一件事。
【解决方案2】:

我采取了一种方法,我计算标签前面,标签中间的数字 - 因为我认为没有格式实际上会改变数字(假设你不添加前导零),然后我根据标签前面的数字或前面和内部的结束标签插入标签

这是代码:

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;
}

输出是

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

【讨论】:

    【解决方案3】:

    我不知道这怎么可能。但无论如何。

        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 
    

    【讨论】:

    • 这可能行不通。这个数字可能很大,“,”几乎可以是任何分隔符(千、百)。语言环境也是如此。所以询问字符串的长度并不总是你所期望的
    • 那么请提供一个无法做到的实际例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 2014-11-10
    • 2022-06-17
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多