This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

RegEx Problems

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Im having trouble with regex, i am trying to match elements of an arraylist with another arraylist of objects. Have tested some simple code that works on just matching a string but when i try and incorporate collections i run into trouble.
The code is as follows:-
public boolean matchPrograms()
{
//ArrayList<Program> todaysProgs = new ArrayList<String>();
//todaysProgs.add("eastenders");
//todaysProgs.add("Corrie");

//ArrayList<Program> matchedPrograms = new ArrayList<Program>();

fc.createFullListings();
todaysPrograms = fc.getProgramsToMatch();
matchActivator.setUsersFavourites();
boolean matchedProg = false;
favIn = matchActivator.getUsersFavourites();


Iterator i = todaysPrograms.iterator();
ArrayList<String> usersPrograms = favIn.getFavProgs();
System.out.println("Users favourites found : " + usersPrograms.size());
Iterator it = usersPrograms.iterator();
System.out.println("Listing located are:- " + todaysPrograms.size());

try{
while(it.hasNext())
{
String program = (String)it.next();
programPattern = Pattern.compile(program, Pattern.CASE_INSENSITIVE);
while(i.hasNext())
{
Matcher matcher = programPattern.matcher((String)i.next().toString());

if(matcher.find())
{
System.out.println("match");
matchedProg = true;
//matchedPrograms.add(match);
}else
System.out.println("No Match Found!!");
}
}


}catch (PatternSyntaxException pse)
{
System.out.println("Syntax error!!");
System.exit(1);
}return matchedProg;


}//end of method match programs


Does anyone have any thoughts on this, would i be better off using the Jakarta package
Thanks folks
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you want to do here? As written, you are trying to match only some of the user's favorites with some of the scheduled programs. What you really want to do (I assume) is match each user favorite against each scheduled program. Is that correct?

If so, you'll need to iterate one of the lists and within that loop completely iterate the second list for each element of the first. For example, if the user has 3 (1-3) favorites and there are 5 scheduled programs (A-E), you'd match 1 against A-E and then 2 against A-E and then 3 against A-E.

I recommend making the list of user's favorites be the outer loop iteration so you can compile each regex pattern only once.

Also, you should be able to specify Iterator<String> so that you don't have to cast i.next(). You won't need the toString() call either since the array elements are already Strings.
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the lack of clarity in my posting, but you have grasped exactly what i wish to do.
Will try this - Thank you
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Ive re-checked my code and i am calling the users favourite programs as the otta loop. I think the problem lies when i iterate through the current arraylist of Program objects.
I have put comments in my code to try and explain what im attempting to do, but i think its a casting issue, would be grateful for any help

Iterator i = todaysPrograms.iterator();//this gets an arraylist of programs

ArrayList<String> usersPrograms = favIn.getFavProgs();
//this retrieves favourites programs stored by user

System.out.println("Users favourites found : " + usersPrograms.size());
//Test to c if programs are there

Iterator<String> it = usersPrograms.iterator();
//Iterator for Users set of programs(Arraylist<String>

System.out.println("Programs located are:- " + todaysPrograms.size());
//Test to c if currents days programs are there

try{
while(it.hasNext())//Outta Loop - Users favourite programs
{
String program = (String)it.next();
//I am trying to cast a Program object into a String to perform
// Matching - i think this is the problem

programPattern = Pattern.compile(program, Pattern.CASE_INSENSITIVE);
//compile as pattern

while(i.hasNext())
// inner loop for all programs currently held in ArrayList<Program>
{
Matcher matcher = programPattern.matcher((String)i.next().toString());

if(matcher.find())
{
System.out.println("match");
matchedProg = true;
//matchedPrograms.add(match);
}else
System.out.println("No Match Found!!");
}
}
Thank you so much, this is driving me mad
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the future, please put [ CODE ] ... [ /CODE ] tags (no spaces inside []s) around your code to retain formatting and indentation. It makes it easier to read.

Okay, the main problem is that you only acquire the "all programs" iterator once. You need to get it each time through the outer loop.

I think the problem lies when i iterate through the current arraylist of Program objects. . . . i think its a casting issue

Can you describe the problem? Are you getting a ClassCastException or some other exception? Or are you obtaining incorrect results. Certainly the above bug I explained will cause you to miss matches, but is there a runtime error other than this that you're experiencing?

Please try to be explicit when stating "there is a problem," otherwise it's hard to help you!
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow thank you so much , its works. Im really grateful for your help. Thanks a million.
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Can i ask one last question, i can obtaim the program title from the matcher.group()method, what im trying to do is when a match is found i need to capture the program object that fires the match
ie. a Program object has a startTime, Title, Desc..
Its late now so im probably not thinking clearly but is there a was of backtracking to capture all attributes of the Program or am i stuck with the title?
Thanks again,
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ive sorted the problem, how silly do i feel,
the following code does exactly what i need, ur a star thank u for ur help
<Code>
if(matcher.matches())
{
System.out.println("match");
System.out.println(matcher.group());
Program matchedProgs = ((Program)i.next());
System.out.println("Program Matched is :- " + matchedProgs);
matchedProg = true;
//matchedPrograms.add(match);
}//else
//System.out.println("No Match Found!!");
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Melanie Walsh:
Can i ask one last question, i can obtaim the program title from the matcher.group() method, what im trying to do is when a match is found i need to capture the program object that fires the match

Okay, but then I start charging you!

From your code, it's a little difficult to determine the full structure used to store the Programs. You clearly have an ArrayList<String>, but is that built from an ArrayList<Program> as implied by the first few commented-out lines of your code?

One option is to maintain a Map<String,Program> that you can use to lookup a Program given the matched title. Otherwise, describe your data structures and I can give you some more ideas.
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I think i have sorted that problem by using a ListIterator and casting the result of previous() back to a program object. Its working 'sort of' but whats happing now is it seems to be stuck on an endless loop. Im using the find() method to locate programs that match a users interest, trouble is when it finds something it just keeps matching the same thing over and over.
Im obviuosly not breaking out of the loop but as yet i cant find a way aroung it, thank you so much for ur input, its been great
Have the following code, can you help
<Code>
public ArrayList<Program> matchInterests()
{

fc.createFullListings();
todaysPrograms = fc.getProgramsToMatch();
matchActivator.setUsersFavourites();
boolean matchedProg = false;
favIn = matchActivator.getUsersFavourites();



ArrayList<String> usersInterests = favIn.getInterest();//this retrieves the users list of Interests
System.out.println("Users Interests found : " + usersInterests.size());//Test to c if interests are there

Iterator<String> it = usersInterests.iterator();//Iterator for Users List of Interests(Arraylist<String>

System.out.println("Programs located are:- " + todaysPrograms.size());//Test to c if currents days programs are there

try{
while(it.hasNext())//Outta Loop - Users Interests
{

String interest = (String)it.next();

programPattern = Pattern.compile(interest, Pattern.CASE_INSENSITIVE);//compile as pattern

ListIterator i = todaysPrograms.listIterator();//this gets an arraylist of programs
while(i.hasNext())// inner loop for all programs currently held in ArrayList<Program>
{

Program tempProg = (Program)i.next();
String dailyProgram = tempProg.getDescription();
Matcher matcher = programPattern.matcher(dailyProgram);

if(matcher.find())
{
System.out.println("match");
System.out.println(matcher.group()); //get the group that fired the match
Program matchedProgs = ((Program)i.previous()); //Cast the matched program back to a temp program
System.out.println("Program Matched is :- " + matchedProgs);
matchedPrograms.add(matchedProgs);//add each match to new array list - yippee
System.out.println("The following program have been found:- " + matchedPrograms.toString());
matchedProg = true;
//matchedPrograms.add(match);
}//else
//System.out.println("No Match Found!!");
}
}


}catch (PatternSyntaxException pse)
{
System.out.println("Syntax error!!");
System.exit(1);
}
return matchedPrograms;
</code>

Thank you
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!