• 0

[JAVA] Regular Expression


Question

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

  • 0

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

  • 0

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 by eitch
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.