donchen Posted February 22, 2009 Share Posted February 22, 2009 Hi guys, I am pretty new to regular expression and hope someone can give me some pointers. For example if i have the following String "I have an apple" "You have two Apples" "I give you one more orange" And when the user enter a text "Apple" to search, using regular expression the results should give "I have an apple" and "You have two Apples" So how will my regular expression look like? Link to comment Share on other sites More sharing options...
0 robz0rz Posted February 23, 2009 Share Posted February 23, 2009 *Apple* [edit] I just noticed you want non-case-sensitive searching... Then I'm not sure how to solve it through pure regexp Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted February 23, 2009 Veteran Share Posted February 23, 2009 Since the search isn't case-sensitive, just convert everything to lower-case. Then you can just do a regular search for "apple" in each input string. Using regular expressions would be possible but quite overkill in this scenario. Link to comment Share on other sites More sharing options...
0 JamesCherrill Posted February 23, 2009 Share Posted February 23, 2009 Compile your Pattern with the CASE_INSENSITIVE flag? http://java.sun.com/j2se/1.4.2/docs/api/ja....String,%20int) If you're not Compiling the regex, just put (?i) at the beginning. Link to comment Share on other sites More sharing options...
0 eitch Posted February 23, 2009 Share Posted February 23, 2009 (edited) I don't know how different they are in java, but here's the javascript equivalent. See if it helps: var fruitStuff = new Array("I have an apple", "You have two Apples", "I give you one more orange"); var pattern = /[^a-zA-Z0-9][aA]pple/; // this one causes strings starting with "Apple" to test nagative, like "Apples and oranges". var pattern = /[aA]pple/; // this one causes strings like "aapple", "tapple", etc to test positive for(sentence in fruitStuff) { if(pattern.test(fruitStuff[sentence])) { document.writeln(fruitStuff[sentence]); } } Edited February 23, 2009 by eitch Link to comment Share on other sites More sharing options...
0 eitch Posted February 24, 2009 Share Posted February 24, 2009 The regex from my previous post fails under certain conditions. Here's an updated version I got from somewhere else: /\bapples?\b/i Link to comment Share on other sites More sharing options...
Question
donchen
Hi guys,
I am pretty new to regular expression and hope someone can give me some pointers.
For example if i have the following String
"I have an apple"
"You have two Apples"
"I give you one more orange"
And when the user enter a text "Apple" to search, using regular expression the results should give
"I have an apple" and "You have two Apples"
So how will my regular expression look like?
Link to comment
Share on other sites
5 answers to this question
Recommended Posts