Results 1 to 10 of 10
  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default Using regular expressions for search

    I currently have a search in my program that simply uses .equalsIgnoreCase, which means that the user must type the exact string.

    So basically I want to make it so that the results for 'ink loyd' will include 'Pink Floyd'. I have looked into regular expressions before but I can't really remember enough to know if they are what I want to use. So what I was thinking was that I could split all the search terms. Then with each one, add wildcards on either side of the string when I parse it into equalsIgnoreCase. Then obviously each one would have to be true for that element to be shown.

    Or is there an even easier way?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    hm, matching "ink loyd" to "Pink Floyd" is going to be tough without first defining as precisely as possible your match characteristics. I don't think that regex is a solution here per se. It looks like you're looking for (for lack of a better term) an AI solution.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default indexOf() ?

    indexOf() method? Something like:

    Java Code:
    String ink = "ink Floyd";
    String pink = "Pink Floyd";
    if (!pink.indexOf(ink)==-1)
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    While "ink Floyd" is easily found, "ink loyd" is not. This remains a most difficult problem (for me) to solve. Hopefully someone else will come up with a better idea.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I agree that first we need to know what a match is.

    One approach might be to break up the two strings into words and find the total "levenshtein" distance between the pairs of words. This a metric for deciding how similar strings are based on the number of missing/added/swapped letters. Mostly what it has going for it is that it is a standard technique and therefore someone else will have implemented it...

  6. #6
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    I basically want it to be like any iTunes/other music organiser's search. If you enter a search with only one term, 'ink', it will search for any entries that have that anywhere in them. I think indexOf could work quite well.

    Then if you enter two search terms, 'ink loyd', I think it will be easy to split this into 'ink' and
    'loyd' and then do an indexOf check with both of these. Then both must be true for the entry to be displayed, i.e. there must be an instance of 'ink' somewhere in the entry as well as an instance of 'loyd'. That seems pretty straight forward to me.

    Does indexOf have an IgnoreCase version? Sorry, I don't have access to the API now.

  7. #7
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    So to answer your question about match characteristics, each search term must be present at least once anywhere in that entry. So 'eat lean' will return 'Eleanor Rigby' by 'The Beatles'...

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    find space and replace w/ ".*"
    also, if necessary add to end and begining as well.
    then use as str pattern in String.matches().

    e.g.
    Java Code:
    final String find = "ink loyd";
    final String[] names = 
      {"Pink Floyd","a b c","Link Ploydxx","no match","Kink Tiloydzz"};
    
    String pattern = ".*"+find.replace(" ",".*")+".*";
    [edit]
    this matches:
    Pink Floyd
    Link Ploydxx
    Kink Tiloydzz
    [/edit]
    Last edited by angryboy; 05-23-2009 at 07:55 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    So 'eat lean' will return 'Eleanor Rigby' by 'The Beatles'...
    you want it disordered? use sets then.
    search for eat, from that set, search for lean (discard rest).
    search for lean, from that set, search for eat (discard rest).
    join both sets.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Essentially agree with @angryboy. A minor modification I'd make is to replace specifically " +" (i.e. any sequence of one or more spaces) with the ".*", just in case.

    Just one thing to consider is whether the user actually wants, say, "loyd" to match Floyd. It might be worth at least giving them the option to match only on starts of words (in which case, replace " +" with ".*\\b" to get the regex).

Similar Threads

  1. Regular Expressions in java
    By blue404 in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2008, 03:43 AM
  2. Using Quantifiers in regular expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-10-2008, 10:43 AM
  3. Handling regular expressions using Regex
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 01-07-2008, 12:46 PM
  4. Capturing Groups using regular expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 11:19 AM
  5. Regular expressions quantifiers
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 11:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •