Java 字符串模式检测

Java 字符串模式检测,java,match,Java,Match,我想将字符串转换为具有如下指定模式的html标记: Walking in the !boldstreet 我想检测!在字符串中加粗,并将以下单词转换为“街道” String s=“在街上行走” 字符串结果=null; 字符串[]样式={“!bold”、“!italic”、“!deleted”、“!marked”}; String[]标记={“b”、“i”、“del”、“mark”}; 列表索引器=新的ArrayList(Arrays.asList(tags)); int指数=0; 用于(字符串

我想将字符串转换为具有如下指定模式的html标记:

Walking in the !boldstreet
我想检测
!在字符串中加粗
,并将以下单词转换为“
街道

String s=“在街上行走”
字符串结果=null;
字符串[]样式={“!bold”、“!italic”、“!deleted”、“!marked”};
String[]标记={“b”、“i”、“del”、“mark”};
列表索引器=新的ArrayList(Arrays.asList(tags));
int指数=0;
用于(字符串x:样式){
如果(s.startsWith(x)){
结果=样式(s,indexer.get(index));
打破
}
索引++;
}
如果(result==null)result=s//没有格式化
返回结果;
//风格方法
公共字符串样式(字符串文本、字符串标记){
返回“+text+”;
}
这是可行的,但当我把这样的东西传给它时:“行走!删除!在街上”

只有它会转!将街道转换为html标记。我如何才能将所有类似的内容转换为html标记?

尝试以下模式:

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();
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);
  }
}
或者它可能只是一张预先制作好的地图。(映射可能更好,因为您可以通过连接其键来构建模式,这样就不会有保持模式和查找同步的问题)。

尝试以下模式:

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

或者它可能只是一张预先制作好的地图。(映射可能更好,因为您可以通过连接其键来构建模式,这样您就不会有保持模式和查找同步的问题)。

不像使用正则表达式那样花哨,但可以完成以下任务:

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();
HashMap tagsAndStyles=newhashmap();
tagsAndStyles.put(“!deleted”,“del”);
tagsAndStyles.put(“!bold”,“b”);
tagsAndStyles.put(“!italic”,“i”);
标记和样式。放置(“!标记”,“标记”);
String input=“Walking!deletedin the!boldstreet”;
StringBuilder=新的StringBuilder();
for(字符串段:input.split(“”){
对于(字符串样式:tagsAndStyles.keySet())
if(段包含(样式)){
String tag=tagsAndStyles.get(style);
segment=“”+segment.substring(style.length())+“”;
打破
}
builder.append(段+“”);
}
builder.toString().trim();

没有使用正则表达式那么花哨,但可以完成任务:

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();
HashMap tagsAndStyles=newhashmap();
tagsAndStyles.put(“!deleted”,“del”);
tagsAndStyles.put(“!bold”,“b”);
tagsAndStyles.put(“!italic”,“i”);
标记和样式。放置(“!标记”,“标记”);
String input=“Walking!deletedin the!boldstreet”;
StringBuilder=新的StringBuilder();
for(字符串段:input.split(“”){
对于(字符串样式:tagsAndStyles.keySet())
if(段包含(样式)){
String tag=tagsAndStyles.get(style);
segment=“”+segment.substring(style.length())+“”;
打破
}
builder.append(段+“”);
}
builder.toString().trim();

假设每个
!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(" "));
static String[]style={“!bold”、“!italic”、“!deleted”、“!marked”};
静态字符串[]标记={b”,“i”,“del”,“mark”};
//此方法仅为*一个*单词设置样式
公共静态字符串样式字(字符串字){
对于(int i=0;i
假设每个
!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(" "));
static String[]style={“!bold”、“!italic”、“!deleted”、“!marked”};
静态字符串[]标记={b”,“i”,“del”,“mark”};
//此方法仅为*一个*单词设置样式
公共静态字符串样式字(字符串字){
对于(int i=0;i
您正在使用s.startsWith(x),它将检测字符串是否以您正在检查的样式开头。您应该使用s.contains(x)来查找字符串中任何位置的样式。使用s.startsWith(x)将检测字符串是否以您正在检查的样式开头。您应该使用s.contains(x)查找字符串中任何地方出现的样式