rrrr
YaBB Newbies
Offline
Posts: 41
Re: please help. regex syntax?
Reply #2 - Mar 8th , 2009, 8:31pm
thanks! it works great. i still have trouble writing regex's myself cause it's just hard for me to put the combos together. i just have an additional question--in addition to the found words from the previous code, i would also like to print out the whole sentence that the words of color are in. for example: "the sun is hot. i am wearing a grey hoody and a hat. i am 2 today." would return: ""i am wearing a grey hoody and a hat." these sentences are not set, static sentences, but are RSS posts that are streaming in. some people don't put periods at the end of the sentences, but just returns to a newline, which could be a problem cause the entire post will be read instead of just that sentence. if it is too difficult to handle, i might not mind reading that entire post then. My code is working, except for the areas where it says "//new": //only taking putting one entry thru at a time for (int i=0; i<num_colors; i++){ //find colors in array first //colorPattern[i] = Pattern.compile("\\s*([\\w'-]+)\\s+" + colorNames[i] + "(?:\\s*([\\w'-]+)?)", Pattern.CASE_INSENSITIVE); //old colorPattern[i] = Pattern.compile("[\\d\\w'-]\\s*([\\w'-]+)\\s+" + colorNames[i] + "(?:\\s*([\\w'-]+)?[\\d\\w'-]\\b)", Pattern.CASE_INSENSITIVE); //incorrect syntax for new? colorMatch[i] = colorPattern[i].matcher(title+description); //look in title & description that match any colors //only if there is a color that matches, get word & find the hood it belongs to... if (colorMatch[i].find()){ drawWord = colorMatch[i].group(); //draw the ENTIRE sentence that the color word is in <<<<<<<<<<<<<<<<<<<<<< //drawSentence = colorMatch[i].group(); //how to draw entire sentence? //draw color hex code drawColor = colorCodes[i]; //assign color name location to color #code String drawColorColor = colorNames[i]; println("Colorcode: "+ drawColor); println("Color: "+ drawColorColor); //draw words surrounding the found color word if (colorMatch[i].group(2) != null){ drawWord = colorMatch[i].group(2); println("Word After: "+ drawWord); //print following word }else{ drawWord = colorMatch[i].group(1); println("Word BEFORE: "+ drawWord); } //find hoods for (int k = 0; k < num_hoods; k++){ hoodsPattern[k] = Pattern.compile("\\b"+hoodsNames[k], Pattern.CASE_INSENSITIVE); //look for any words within findHoods array hoodsMatch[k] = hoodsPattern[k].matcher(title); //look in titles to match any neighborhoods if(hoodsMatch[k].find()){ drawHood = k; //take the hood number and put it into integer drawHood println("Hood: " + hoodsMatch[k].group()); //dots.add(new DrawDot(drawHood, drawColor, drawWord)); //old dots.add(new DrawDot(drawHood, drawColor, drawWord, drawSentence)); //new counter++; } //end if colorMatch find hoods } } //end colorMatch[i].find()) } //end for loop for find colors in array println("# found colors counter: " + counter + newline);