【问题标题】:How to find an expression and replace it with certain text?如何查找表达式并将其替换为某些文本?
【发布时间】:2016-10-04 00:54:23
【问题描述】:

我有一个在句子中间带有标记的 xml 文件。 例如:#his/her_caps# 测试完成。

我想在 xml 文件中搜索任何 #(text)# 标记并将其替换为其正确的代词,因此我将上面的标记替换为 His 或 Her。如何搜索 #(text)# 表达式?

如果我要使用分词器,我不明白如何使用它,也不知道如何正确地使用正则表达式。

我正在完成一个别人开始的项目,这就是他们所拥有的,但他们无法让它发挥作用。我只想知道如何在 xml 文件中搜索标签。

尝试一:

File inputXML = new File("template.xml"); // creates new input file
        DocumentBuilderFactory parser = DocumentBuilderFactory.newInstance(); // new instance of doc builder
        DocumentBuilder dParser = parser.newDocumentBuilder(); // calls it
        Document doc = dParser.parse(inputXML); // parses file
        doc.getDocumentElement().normalize();

        NodeList pList = doc.getElementsByTagName("Verbiage"); // gets element by tag name and places into list to begin parsing

        int gender = 1; // gender has to be taken from the response file, it is hard coded for testing purposes
        //System.out.println("----------------------------"); // new line

        // loops through the list of Verbiage tags
        for (int temp = 0; temp < pList.getLength(); temp++) {
            Node pNode = pList.item(0); // sets node to temp

            if (pNode.getNodeType() == Node.ELEMENT_NODE) { // if the node type = the element node
                Element eElement = (Element) pNode;
                NodeList pronounList = doc.getElementsByTagName("pronoun"); // gets a list of pronoun element tags

                if (gender == 0) { // if the gender is male

                    int count1 = 0;
                    while (count1 < pronounList.getLength()) {

                        if ("#resp_he/she_lc#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("he");
                        }

                        if ("#resp_he/she_caps#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("He");
                        }

                        if ("#resp_his/her_lc#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("his");
                        }
                        if ("#resp_his/her_caps#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("His");
                        }

                        if ("#resp_him/her_lc#".equals(pronounList.item(count1).getTextContent())) {
                            pronounList.item(count1).setTextContent("him");
                        }
                        count1++;
                    }
                    pNode.getNextSibling();

                } else if (gender == 1) { // female
                    int count = 0;
                    while (count < pronounList.getLength()) {

                        if ("#he/she_lc#".equals(pronounList.item(count).getTextContent())) {
                            pronounList.item(count).setTextContent("she");
                        }

                        if ("#he/she_caps#".equals(pronounList.item(count).getTextContent())) {
                            pronounList.item(count).setTextContent("She");
                        }

                        if ("#his/her_lc#".equals(pronounList.item(count).getTextContent())) {
                            pronounList.item(count).setTextContent("her");
                        }
                        if ("#his/her_lc#".equals(pronounList.item(count).getTextContent())) {
                            pronounList.item(count).setTextContent("Her");
                        }

                        if ("#him/her_lc#".equals(pronounList.item(count).getTextContent())) {
                            pronounList.item(count).setTextContent("her");
                        }
                        count++;
                    }
                    pNode.getNextSibling();
                }
            }
        }

【问题讨论】:

  • 您能帮我们解决您尝试过的代码吗?
  • xmlString = xmlString.replace("##his/her_caps##", "HER"); ?
  • @Tauqir 我还没找到怎么做,所以我在这里哈哈。我不知道是否使用分词器或是否有其他方法来搜索表达式。
  • @Felicia btw,一种将文件作为字符串读取的方法:String content = new String(Files.readAllBytes(Paths.get("template.xml")));
  • 对于涉及转换 XML 的大多数任务,您应该研究 XSLT。

标签: java xml parsing expression tokenize


【解决方案1】:

在记事本++中使用正则表达式

^#.{0,}#$ 应该找到#之间的所有东西

不记得 # 是否需要转义(#)。我不这么认为。

另外,如果您需要专门找到他或她,您可以添加。 ^#.{0,}他的.{0,}#$

【讨论】:

  • 如果您使用 ^#(.{0,})his(.{0,})#$。要找到它,您可以将其替换为 #\1His\2#
  • 我不知道正则表达式是如何工作的......这是我的问题的一部分哈哈。 notepad++ 中的正则表达式怎么办?
  • 使用搜索/替换功能我认为它在顶部的编辑中。
  • 使用 ^#(.{0,})His(.{0,})#$ 进行搜索。首先要确保它选择了正确的标签。并确保您检查了正则表达式
  • 代码必须能够自己进行搜索,而不是手动进行。
猜你喜欢
  • 1970-01-01
  • 2016-07-04
  • 1970-01-01
  • 2011-03-22
  • 1970-01-01
  • 2018-09-05
  • 2021-08-14
  • 2013-04-11
  • 1970-01-01
相关资源
最近更新 更多