java中以下条件的模式

时间:2010-05-29 12:28:22

标签: java regex

我想知道如何写模式..

例如:

这个词是

   "AboutGoogle AdWords Drive traffic and customers to your site. Pay through Cheque,      Net Banking or Credit Card. Google Toolbar Add a search box to your browser. Google SMS To find out local information simply SMS to 54664. Gmail Free email with 7.2GB storage and less spam. Try Gmail today. Our ProductsHelp Help with Google Search, Services and ProductsGoogle Web Search Features Translation, I'm Feeling Lucky, CachedGoogle Services & Tools Toolbar, Google Web APIs, ButtonsGoogle Labs Ideas, Demos, ExperimentsFor Site OwnersAdvertising AdWords, AdSenseBusiness Solutions Google Search Appliance, Google Mini, WebSearchWebmaster Central One-stop shop for comprehensive info about how Google crawls and indexes websitesSubmit your content to Google Add your site, Google SitemapsOur CompanyPress Center News, Images, ZeitgeistJobs at Google Openings, Perks, CultureCorporate Info Company overview, Philosophy, Diversity, AddressesInvestor Relations Financial info, Corporate governanceMore GoogleContact Us FAQs, Feedback, NewsletterGoogle Logos Official Logos, Holiday Logos, Fan LogosGoogle Blog Insights to Google products and cultureGoogle Store Pens, Shirts, Lava lamps©2010 Google - Privacy Policy - Terms of Service"

我必须搜索一些字......

例如“google insights”

那么如何在java中编写代码......

我只是写小代码......

检查我的代码并回答我的问题...

该代码仅用于查找搜索词,在哪里。

但是我需要在搜索词的前面显示一些单词并在搜索后面显示一些单词......

类似于谷歌搜索...

我的代码是

Pattern p = Pattern.compile("(?i)(.*?)"+search+"");
Matcher m = p.matcher(full);
String title="";
while (m.find() == true) 
{
  title=m.group(1);
  System.out.println(title);
} 

完整的是原始内容,搜索搜索词......

谢谢并提前

3 个答案:

答案 0 :(得分:1)

正则表达式不能用于任何东西,它们很强大,但即使是正则表达式也有其自身的局限性。下面的代码查找特定的单词:

    public static void main(String[] args)
    {
        String str = "AboutGoogle AdWords Drive traffic and customers to your site. Pay through Cheque,      Net Banking or Credit Card. Google Toolbar Add a search box to your browser. Google SMS To find out local information simply SMS to 54664. Gmail Free email with 7.2GB storage and less spam. Try Gmail today. Our ProductsHelp Help with Google Search, Services and ProductsGoogle Web Search Features Translation, I'm Feeling Lucky, CachedGoogle Services & Tools Toolbar, Google Web APIs, ButtonsGoogle Labs Ideas, Demos, ExperimentsFor Site OwnersAdvertising AdWords, AdSenseBusiness Solutions Google Search Appliance, Google Mini, WebSearchWebmaster Central One-stop shop for comprehensive info about how Google crawls and indexes websitesSubmit your content to Google Add your site, Google SitemapsOur CompanyPress Center News, Images, ZeitgeistJobs at Google Openings, Perks, CultureCorporate Info Company overview, Philosophy, Diversity, AddressesInvestor Relations Financial info, Corporate governanceMore GoogleContact Us FAQs, Feedback, NewsletterGoogle Logos Official Logos, Holiday Logos, Fan LogosGoogle Blog Insights to Google products and cultureGoogle Store Pens, Shirts, Lava lamps©2010 Google - Privacy Policy - Terms of Service";
        String searchWord = "your";

        int loc = 0;
        loc = str.indexOf(searchWord);
        while (loc != -1)
        {
            loc = str.indexOf(searchWord, loc + searchWord.length());
            System.out.println("found");
        }
    }

以下是输出:

  

发现

     

发现

     

发现

     

结果

希望有所帮助。

答案 1 :(得分:1)

最好的解决方案必须使用非常复杂的字符串搜索和索引算法。如果你不关心性能,那么很容易实现:

import java.util.*;
public class SearchAndContext {
    public static void main(String[] args) {
        String text = "The path of the righteous man is beset on all sides by "
        + "the iniquities of the selfish and the tyranny of evil men. Blessed "
        + "is he, who in the name of charity and good will, shepherds the "
        + "weak through the valley of darkness, for he is truly his brother's "
        + "keeper and the finder of lost children. And I will strike down "
        + "upon thee with great vengeance and furious anger those who would "
        + "attempt to poison and destroy my brothers. And you will know my "
        + "name is the Lord when I lay my vengeance upon thee.";

        List<String> words = Arrays.asList(text.split(" "));
        final int W = 3;
        final int N = words.size();
        String[] queries = { "vengeance", "and", "monkeys" };
        for (String query : queries) {
            List<String> search = words;
            System.out.println("Searching for " + query);
            for (int idx = -1, pos; (pos = search.indexOf(query)) != -1; ) {
                idx += (pos+1);
                int left = Math.max(0, idx - W);
                int right = Math.min(N, idx + W + 1);
                System.out.println(words.subList(left, right));
                search = search.subList(pos+1, search.size());
            }
        }
    }
}

打印:

Searching for vengeance
[thee, with, great, vengeance, and, furious, anger]
[I, lay, my, vengeance, upon, thee.]
Searching for and
[of, the, selfish, and, the, tyranny, of]
[name, of, charity, and, good, will,, shepherds]
[his, brother's, keeper, and, the, finder, of]
[with, great, vengeance, and, furious, anger, those]
[attempt, to, poison, and, destroy, my, brothers.]
Searching for monkeys

正如您所看到的,这会查找搜索查询的出现次数,并且还会在“匹配”周围提供最多W=3个字词的上下文。

API参考

答案 2 :(得分:0)

模式被注意到,而不是创建。对于存在的模式,您必须注意到许多问题以类似的方式解决。

一旦你看到相同的代码结构一次又一次地发生,那么你就找到了一个模式。如果您记录代码的结构,并记录它在解决问题方面的优势及其解决问题的弱点,那么您将记录该模式。记录模式可以更轻松地与其他人共享模式,并使以后更容易重用模式。