Обнаружение строкового шаблона - PullRequest
2 голосов
/ 28 апреля 2020

Я хочу превратить строку в тег html с указанным шаблоном, например так:

Walking in the !boldstreet

Я хочу обнаружить !bold в строке и превратить следующее слово в "<b>street</b> ".

    String s = "Walking in the !boldstreet" 
    String result = null;
    String[] styles = {"!bold", "!italic", "!deleted", "!marked"};
    String[] tags = {"b", "i", "del", "mark"};
    List<String> indexer = new ArrayList<String>(Arrays.asList(tags));
    int index = 0;
    for (String x: styles) {
        if (s.startsWith(x)) {
            result = style(s, indexer.get(index));
            break;
        }
        index++;
    }
    if (result == null) result = s; //there was no formatting


    return result;

      //style method
      public String style(String text, String tag) {
    return "<" + tag + ">" + text + "</" + tag + ">";
}

Это работает, но когда я передаю ему что-то вроде этого:" Ходьба! Удалено в! Boldstreet "

только оно превратит! , Как я могу сделать так, чтобы все эти вещи превращались в html теги?

Ответы [ 3 ]

3 голосов
/ 28 апреля 2020

Попробуйте шаблон как:

Pattern pattern = Pattern.compile("!(bold|italic|deleted|marked)(.*?)\\b");

А затем выполните замену что-то вроде:

Matcher matcher = pattern.matcher(s);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
  String tag = getTagFor(matcher.group(1));  // Implement lookup of tag from the bit immediately after the !
  String text = matcher.group(2);
  sb.appendReplacement(sb, String.format("<%s>%s</%s>", tag, text, tag));
}
matcher.appendTail(sb);

String result = sb.toString();

getTagFor может быть что-то вроде:

String getTagFor(String code) {
  switch (code) {
    case "bold": return "b";
    case "italic": return "i";
    case "deleted": return "del";
    case "marked": return "mark";
    default: throw new IllegalArgumentException(code);
  }
}

или это может быть просто предварительно построенная карта. (Карта может быть лучше, потому что вы можете построить шаблон, соединив его ключи, чтобы у вас не было проблем с синхронизацией шаблона и поиска c).

1 голос
/ 28 апреля 2020

Предполагая, что каждый !xxx относится только к слову после него, а "слова" разделены пробелом ...

static String[] styles = {"!bold", "!italic", "!deleted", "!marked"};
static String[] tags = {"b", "i", "del", "mark"};

// this method styles *one* word only
public static String styleWord(String word) {
    for (int i = 0 ; i < styles.length ; i++) {
        if (word.startsWith(styles[i])) {
            String rest = word.substring(styles[i].length());
            String cleaned = styleWord(rest); // handles nested tags!
            return style(cleaned, tags[i]);
        }
    }
    return word; // no styles
}
public static String style(String text, String tag) {
    return "<" + tag + ">" + text + "</" + tag + ">";
}

// ...

String s = "Walking !deletedin the !boldstreet";

// splits the string into words
String[] words = s.split(" ");
// styles each word, and joins them all together again.
return Arrays.stream(words).map(EnclosingClass::styleWord).collect(Collectors.joining(" "));
1 голос
/ 28 апреля 2020

Не так сложно, как использование регулярных выражений, но выполняет свою работу:

HashMap<String, String> tagsAndStyles = new HashMap<>();
tagsAndStyles.put("!deleted", "del");
tagsAndStyles.put("!bold", "b");
tagsAndStyles.put("!italic", "i");
tagsAndStyles.put("!marked", "mark");

String input = "Walking !deletedin the !boldstreet";
StringBuilder builder = new StringBuilder();

for (String segment : input.split(" ")) {
    for (String style : tagsAndStyles.keySet())
        if (segment.contains(style)) {
            String tag = tagsAndStyles.get(style);
            segment = "<" + tag + ">" + segment.substring(style.length()) + "</" + tag + ">";
            break;
        }

    builder.append(segment + " ");
}

builder.toString().trim();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...