Java 是否可以使用$1“;作为另一个方法的参数,并将返回的字符串放在其上';这是什么地方?

Java 是否可以使用$1“;作为另一个方法的参数,并将返回的字符串放在其上';这是什么地方?,java,html,regex,replaceall,Java,Html,Regex,Replaceall,我正在编写标记->HTML解析器-用户使用自定义标记键入一些文本(在BB和Markdown之间,如[b]或[\RRGGBB]),然后它解析为HTML-因此[\FF0000]红色文本[/\35;]变成红色文本,然后以HTML样式放入JTextPane HashMap中的所有用户标记和HTML存储以及如下解析: public static String parseBBToHTML(String text) { String html = text; Map<String,St

我正在编写标记->HTML解析器-用户使用自定义标记键入一些文本(在BB和Markdown之间,如
[b]
[\RRGGBB]
),然后它解析为HTML-因此
[\FF0000]红色文本[/\35;]
变成
红色文本
,然后以HTML样式放入JTextPane

HashMap中的所有用户标记和HTML存储以及如下解析:

public static String parseBBToHTML(String text) {
    String html = text;

    Map<String,String> bbMap = new HashMap<String , String>();

    bbMap.put("\\[b\\]", "<strong>");
    bbMap.put("\\[/b\\]", "</strong>");
    ...
    bbMap.put("\\[color=(.+?)\\]", "<span style='color:$1;'>");
    bbMap.put("\\[/color\\]", "</span>");

    for (Map.Entry entry: bbMap.entrySet()) {
        html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
    }

    return html;
}
bbMap.put("\\[\\!(.+?)\\]", Dialogue.pause("$1"));
Pattern tagPattern = Pattern.compile("\\[(/?)([^\\]]*)\\]");
Matcher tagMatcher = tagPattern.matcher( input );    

StringBuffer output = new StringBuffer();
while( tagMatcher.find() ) {
  String closeMarker = tagMatcher.group( 1 );
  String tagContent = tagMatcher.group( 2 );

  //use the tagContent to do the lookup in the map and create the replacement string
  String replacement = ...;

  tagMatcher.appendReplacement( output, replacement );
}
tagMatcher.appendTail( output ); 
bbMap.put("\\[b\\]", s -> "<strong>");
bbMap.put("\\[/b\\]", s -> "</strong>");
//...
bbMap.put("\\[color=(.+?)\\]", s -> "<span style='color:" + s + ";'>");
bbMap.put("\\[/color\\]", s -> "</span>");

bbMap.put("\\[\\!(.+?)\\]", s -> Dialogue.pause(s));

for (Map.Entry entry : bbMap.entrySet()) {
    StringBuffer buffer = new StringBuffer(html.length());

    Matcher matcher =
        Pattern.compile(entry.getKey().toString()).matcher(html);
    while (matcher.find()) {
        String match =
            (matcher.groupCount() > 0 ? matcher.group(1) : null);
        String replacement = entry.getValue().apply(match);
        matcher.appendReplacement(buffer,
            Matcher.quoteReplacement(replacement));
    }
    matcher.appendTail(buffer);

    html = buffer.toString();
}
…但它不起作用。它是在方法对话中给出的。pause字面上是字符串“$1”,而不是作为解析值的
$1


如何使用
$1
作为参数来形成“pause tag”并将其放在文本中?

在Java中,$1在捕获命令之外没有特殊含义,它不是一个全局变量。您需要解析字符串并将组捕获到局部变量中,以便将来使用它

请看这里:

这意味着你目前使用bbMap的方法不适用于这些替代品


您可以这样做:首先运行bbMap修改。然后运行第二组修改bbMap2,首先捕获组,计算长度,根据长度创建所需的字符串,然后替换。

如果调用
bbMap.put(“\\[\!(.+?)\\]”,Dialogue.pause($1”)使用其他代码,您希望实现以下目标:

public static String parseBBToHTML(String text) {
    String html = text;

    Map<String,String> bbMap = new HashMap<String , String>();

    bbMap.put("\\[b\\]", "<strong>");
    bbMap.put("\\[/b\\]", "</strong>");
    ...
    bbMap.put("\\[color=(.+?)\\]", "<span style='color:$1;'>");
    bbMap.put("\\[/color\\]", "</span>");

    for (Map.Entry entry: bbMap.entrySet()) {
        html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
    }

    return html;
}
bbMap.put("\\[\\!(.+?)\\]", Dialogue.pause("$1"));
Pattern tagPattern = Pattern.compile("\\[(/?)([^\\]]*)\\]");
Matcher tagMatcher = tagPattern.matcher( input );    

StringBuffer output = new StringBuffer();
while( tagMatcher.find() ) {
  String closeMarker = tagMatcher.group( 1 );
  String tagContent = tagMatcher.group( 2 );

  //use the tagContent to do the lookup in the map and create the replacement string
  String replacement = ...;

  tagMatcher.appendReplacement( output, replacement );
}
tagMatcher.appendTail( output ); 
bbMap.put("\\[b\\]", s -> "<strong>");
bbMap.put("\\[/b\\]", s -> "</strong>");
//...
bbMap.put("\\[color=(.+?)\\]", s -> "<span style='color:" + s + ";'>");
bbMap.put("\\[/color\\]", s -> "</span>");

bbMap.put("\\[\\!(.+?)\\]", s -> Dialogue.pause(s));

for (Map.Entry entry : bbMap.entrySet()) {
    StringBuffer buffer = new StringBuffer(html.length());

    Matcher matcher =
        Pattern.compile(entry.getKey().toString()).matcher(html);
    while (matcher.find()) {
        String match =
            (matcher.groupCount() > 0 ? matcher.group(1) : null);
        String replacement = entry.getValue().apply(match);
        matcher.appendReplacement(buffer,
            Matcher.quoteReplacement(replacement));
    }
    matcher.appendTail(buffer);

    html = buffer.toString();
}
  • 正则表达式为组1查找匹配项
  • 对话。暂停($1)是用组1(应该是一个数字)的内容调用的
问题是它不能这样工作,所以你可以考虑重构你的代码。与迭代可能的标记(即
bbMap
中的条目)并在输入中查找它们不同,您可能希望迭代输入,获取开始和结束标记并在映射中进行查找

您可以使用正则表达式来实现这一点,但请注意,这与HTML有着相同的问题:除非您真正了解数据的结构等,否则它不适合正则表达式

因此,您可能需要使用表达式查找所有标记,获取标记名/内容,然后进行替换。然后可能会是这样的:

public static String parseBBToHTML(String text) {
    String html = text;

    Map<String,String> bbMap = new HashMap<String , String>();

    bbMap.put("\\[b\\]", "<strong>");
    bbMap.put("\\[/b\\]", "</strong>");
    ...
    bbMap.put("\\[color=(.+?)\\]", "<span style='color:$1;'>");
    bbMap.put("\\[/color\\]", "</span>");

    for (Map.Entry entry: bbMap.entrySet()) {
        html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
    }

    return html;
}
bbMap.put("\\[\\!(.+?)\\]", Dialogue.pause("$1"));
Pattern tagPattern = Pattern.compile("\\[(/?)([^\\]]*)\\]");
Matcher tagMatcher = tagPattern.matcher( input );    

StringBuffer output = new StringBuffer();
while( tagMatcher.find() ) {
  String closeMarker = tagMatcher.group( 1 );
  String tagContent = tagMatcher.group( 2 );

  //use the tagContent to do the lookup in the map and create the replacement string
  String replacement = ...;

  tagMatcher.appendReplacement( output, replacement );
}
tagMatcher.appendTail( output ); 
bbMap.put("\\[b\\]", s -> "<strong>");
bbMap.put("\\[/b\\]", s -> "</strong>");
//...
bbMap.put("\\[color=(.+?)\\]", s -> "<span style='color:" + s + ";'>");
bbMap.put("\\[/color\\]", s -> "</span>");

bbMap.put("\\[\\!(.+?)\\]", s -> Dialogue.pause(s));

for (Map.Entry entry : bbMap.entrySet()) {
    StringBuffer buffer = new StringBuffer(html.length());

    Matcher matcher =
        Pattern.compile(entry.getKey().toString()).matcher(html);
    while (matcher.find()) {
        String match =
            (matcher.groupCount() > 0 ? matcher.group(1) : null);
        String replacement = entry.getValue().apply(match);
        matcher.appendReplacement(buffer,
            Matcher.quoteReplacement(replacement));
    }
    matcher.appendTail(buffer);

    html = buffer.toString();
}

字符串只是一个单一的静态值。您需要的是一个从另一个字符串动态计算的字符串

您需要从以下位置更改地图:

Map<String, String> bbMap = new HashMap<String, String>();
Map bbMap=newhashmap();
为此:

Map<String, UnaryOperator<String>> bbMap = new HashMap<>();
Map bbMap=newhashmap();
UnaryOperator
是一个接受字符串参数并返回字符串值的函数。因此,您可以这样填充地图:

public static String parseBBToHTML(String text) {
    String html = text;

    Map<String,String> bbMap = new HashMap<String , String>();

    bbMap.put("\\[b\\]", "<strong>");
    bbMap.put("\\[/b\\]", "</strong>");
    ...
    bbMap.put("\\[color=(.+?)\\]", "<span style='color:$1;'>");
    bbMap.put("\\[/color\\]", "</span>");

    for (Map.Entry entry: bbMap.entrySet()) {
        html = html.replaceAll(entry.getKey().toString(), entry.getValue().toString());
    }

    return html;
}
bbMap.put("\\[\\!(.+?)\\]", Dialogue.pause("$1"));
Pattern tagPattern = Pattern.compile("\\[(/?)([^\\]]*)\\]");
Matcher tagMatcher = tagPattern.matcher( input );    

StringBuffer output = new StringBuffer();
while( tagMatcher.find() ) {
  String closeMarker = tagMatcher.group( 1 );
  String tagContent = tagMatcher.group( 2 );

  //use the tagContent to do the lookup in the map and create the replacement string
  String replacement = ...;

  tagMatcher.appendReplacement( output, replacement );
}
tagMatcher.appendTail( output ); 
bbMap.put("\\[b\\]", s -> "<strong>");
bbMap.put("\\[/b\\]", s -> "</strong>");
//...
bbMap.put("\\[color=(.+?)\\]", s -> "<span style='color:" + s + ";'>");
bbMap.put("\\[/color\\]", s -> "</span>");

bbMap.put("\\[\\!(.+?)\\]", s -> Dialogue.pause(s));

for (Map.Entry entry : bbMap.entrySet()) {
    StringBuffer buffer = new StringBuffer(html.length());

    Matcher matcher =
        Pattern.compile(entry.getKey().toString()).matcher(html);
    while (matcher.find()) {
        String match =
            (matcher.groupCount() > 0 ? matcher.group(1) : null);
        String replacement = entry.getValue().apply(match);
        matcher.appendReplacement(buffer,
            Matcher.quoteReplacement(replacement));
    }
    matcher.appendTail(buffer);

    html = buffer.toString();
}
bbMap.put(“\\[b\\]”,s->“”;
bbMap.put(“\\[/b\\]”,s->“”;
//...
bbMap.put(“\\[color=(.+?)\\]”,s->”);
bbMap.put(“\\[/color\\]”,s->”);
bbMap.put(“\\[\\!(.+?)\\]”,s->Dialogue.pause(s));
对于(Map.Entry:bbMap.entrySet()){
StringBuffer=新的StringBuffer(html.length());
匹配器匹配器=
Pattern.compile(entry.getKey().toString()).matcher(html);
while(matcher.find()){
字符串匹配=
(matcher.groupCount()>0?matcher.group(1):null);
字符串替换=entry.getValue().apply(匹配);
匹配器。附件替换(缓冲区,
匹配器。报价替换(替换));
}
matcher.appendTail(缓冲区);
html=buffer.toString();
}

这个类是什么:Dialogue.pause
entry.getValue().toString()在暂停情况下返回什么?
Dialogue.pause在我的帖子中。公共静态字符串暂停(字符串ms)等等。
entry.getValue().toString()
不会返回,因为程序已停止运行:线程“main”中的异常java.lang.NumberFormatException:对于输入字符串,java.lang.NumberFormatException.forInputString(未知源代码)位于java.lang.Integer.parseInt(未知源代码)在vengine.Dialogue.pause(Dialogue.java:81)的java.lang.Integer.parseInt(未知源代码)中,抱歉,无法格式化此文件。奇怪。这让我想起了
Matcher#appendReplacement
。但我如何修改我的
parseBBToHTML
?请给出一个向量。我正在用我自己的向量生成输入文本,而且只生成一次,所以不会出现类似
[!@$$arrrawet]
之类的错误。是的,也许像
获取用户文本->查找标记->用HTML标记替换它们这样的代码结构会比把它放在我的parseBBToHTML中并希望得到最好的结果要好。我马上就去试试。哇,太棒了!从来没有听说过使用UnaryOperator的地图!但它不是编译的<代码>应用(字符串)对于类型对象是未定义的
我将使用
字符串替换=((函数)项.getValue()).apply(匹配),但我不认为这是件好事。