如何查找小于和大于之间的文本,然后剥离<>在Java?

时间:2015-10-04 06:00:09

标签: java charat

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

protected void Page_Load(object sender, EventArgs e)
{
    var response = HttpContext.Current.Response;
    if (response.Cookies["ecookie"]["name"] != null) //doesn't go inside this condition since it's null
    {
          string name = response.Cookies["ecookie"]["name"];
    }
}

当我在Google上搜索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 <

>

3 个答案:

答案 0 :(得分:5)

您可以使用PatternMatcher类。

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

答案 1 :(得分:2)

这里真的有两个问题,所以我只回答最后一个问题;当你拥有所需的<text>时,请按以下步骤操作:

String text = "<the_text_you_want>";

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

那将摆脱分界符。

答案 2 :(得分:1)

阅读整行并将其存储,例如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]);