繁体   English   中英

如何在较小和较大之间找到文本,然后在Java中删除<>?

[英]How to find text between less and greater than, then strip the <> in Java?

我不知道如何找到这些单词..例子我有这个文字......

The other day I went to the <location> and bought some <plural-noun> . Afterwards, I went to <location> , but it was very <adjective> so I left quickly and went to <location> .

当我在谷歌搜索<>时,我不知道搜索的原因,它将被忽略。 需要帮助如何获得此字符串。

所以我会得到<location><plural-noun><location><adjective><location>

我必须使用charAt()方法。 我的尝试:

String string = this.fileName;
for(int i = 0; i < string.length(); i++)
                if((string.charAt(i) == '<') && (string.charAt(i) == '>'))
                    System.println(""); //<-------- IM STUCK HERE

我不知道......差不多两天没睡觉了。

我目前但最后一个问题......如何删除显示中每个单词的<>

String string = this.template;
        Pattern pattern = Pattern.compile("<.*?>");
        Matcher matcher = pattern.matcher(string);

        List<String> listMatches = new ArrayList<String>();

        while(matcher.find()) {
            listMatches.add(matcher.group());
        }
        // System.out.println(listMatches.size());
        int indexNumber = 1;
         for(String s : listMatches) {
             System.out.println(Integer.toString(indexNumber) + ". " + s);
             indexNumber++;
         }

您可以使用PatternMatcher类。

  1. 搜索正则表达式<.*?>
  2. 使用Matcher查找模式。

这里真的有两个问题,所以我只回答最后一个问题; 如果你有你想要的<text> ,那就这样:

String text = "<the_text_you_want>";

text.replace("<","").replace(">","").replace("-"," ");

这将摆脱分界符。

读取整行并将其存储在String line 然后,使用:

String line = "The other day I went to the <location> and bought some <plural-noun> . Afterwards, I went to <location> , but it was very <adjective> so I left quickly and went to <location> ."; 

boolean found = false;
String data[] = new String[20];
int counter = 0;

Arrays.fill(data, "");

for(int i = 0; i < line.length() && counter < 20; i++) {
    if(line.charAt(i) == '<')
        found = true;
    else if(line.charAt(i) == '>' && found) {
        found = false;
        counter++;
    }
    else if(found) {
        data[counter] += line.charAt(i);
    }
}

for(int i = 0; i < counter; i++)
    System.out.println("Scanned data #" + (i + 1) + " = " + data[i]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM