tags:

views:

63

answers:

3

hi

i want to know how to write pattern..

for example :

the word is

   "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"

I have to search some word...

for example "google insights"

so how to write the code in java...

i just write small code...

check my code and answer my question...

that code only use for find the search word, where is that.

but i need to display some words front of search word and display some words rear of search workd...

similar to google search...

my code is

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);
} 

the full is orignal content, search s search word...

thanks and advance

A: 

Patterns are noticed, not created. For a pattern to exist, you must notice that a number of problems are solved in a similar way.

Once you see that the same structure of code happens again and again, then you have found a pattern. If you document the structure of code, and make note of its strength in solving the problem and its weaknesses in solving the problem, then you will have documented the pattern. Documenting the pattern makes it easier to share the pattern with others and makes it easier to reuse the pattern later.

Edwin Buck
...meaning *design* patterns, of course. This is why I always delete tags with the word "pattern" in them from regex questions.
Alan Moore
+1  A: 

Regular expressions can't be used for anything, they are powerful, but even regular expressions have their own limitations. The code below looks for a specific word:

    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");
        }
    }

The following is the output:

found

found

found

found

Hope that helps.

npinti
hi i want to get the "Drive traffic and customers to <b>your</b> site. Pay through Cheque," if i search the word is "your" can u understand what i am asking?bcs i have some communication problem thats y i cant explain correctly.
zahir hussain
Simply replace the word "your" with the word, or words, you are looking for. For instance, you can do String searchWord = "Drive traffic and customers to your site. Pay through Cheque,". Then, process the text in the while loop just before the value of loc is updated.
npinti
+1  A: 

The best solution would have to use really sophisticated string search and indexing algorithms. If you don't care about performance, something like this is quite easy to implement:

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());
            }
        }
    }
}

This prints:

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

As you can see, this finds occurrences of the search query, and also provides the context of up to W=3 words around the "hit".

API references

polygenelubricants