Stephan van Hulst wrote:Why do you want to use a regex? Is it not allowed for "tree" or "ski" to be anywhere in the input, or just not in the part between ice and snow? What if there are multiple instances of the words ice and snow? Do you also want text between "ice" and "ice"? Can the order of "ice" and "snow" be reversed?
Please give us more information on the requirements and circumstances you're working with, and why you're trying to achieve this in the first place.
Stephan van Hulst wrote:Well, you definitely don't want to do this with one regex. Even when you break it up it's going to look really ugly. Take a look:
mark smith wrote:on the result, i can loop to detect every word and display them only if the word have more then 3 letter..... but is there a way to do it directly in the regex?
Henry Wong wrote:
mark smith wrote:on the result, i can loop to detect every word and display them only if the word have more then 3 letter..... but is there a way to do it directly in the regex?
Ignore me if I seem to be the only one ... but having read the topic posts, I am still not clear what is being asked for here. Could you show us a bunch of examples? Input, and expected output?
Henry
mark smith wrote:
ski and tree need to be there to be bad....
snow hello house ice ski tree -> bad
snow the hello house ice ski-> return word between snow and ice who have more then 3 letters so -> hello and house is returned
snow the hello house ice -> return word between snow and ice who have more then 3 letters so -> hello and house is returned
need to work fine except when only one of the two bad word are there.....
mark smith wrote:
don,t seem to work correctely because if the input = ice hello house snow tree ski
that work.... but it should not because tree and ski is available....
mark smith wrote:
on the result, i can loop to detect every word and display them only if the word have more then 3 letter..... but is there a way to do it directly in the regex?
mark smith wrote:
i'm not a regex expert, but i think that could take less time to write a regex than to write a function to do the same thing
mark smith wrote:
to get only word of 3 letter and more i tried without success:
Henry Wong wrote:
mark smith wrote:
on the result, i can loop to detect every word and display them only if the word have more then 3 letter..... but is there a way to do it directly in the regex?
Not really. Regexes is not really good at returning an unknown number of matches with a single match. You have to rewrite it to loop yourself -- and probably to check the edge tokens yourself too.
Henry Wong wrote:
I guess another way to do this is... use regex to capture the phrase between the two edges, then use regex on the phrase to get all words greater than three letters.
Henry
mark smith wrote:
(.*) is the regex who capture the sentence between the two edge, no?
Henry Wong wrote:
mark smith wrote:
(.*) is the regex who capture the sentence between the two edge, no?
Yes. it captures the phrase between the two edges. With it, you can use another regex to get the words that are greater than four letters. You will not be able to capture the words in the same pass, because you have an indeterminate number of words.
Henry
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:Edit: I misread the splittedWords pattern. It wouldn't cause "hello", "house" and "test" to be found, but instead empty strings just before those words. After all, you're using a positive lookahead.
Rob Spoor wrote:matcher.group(1) is " the hello house test " (including leading and trailing spaces). That certainly does not match your splittedWords pattern. It could find a few results ("hello", "house", "test"), but that's not what you're doing right now.
Edit: I misread the splittedWords pattern. It wouldn't cause "hello", "house" and "test" to be found, but instead empty strings just before those words. After all, you're using a positive lookahead.
mark smith wrote:this code should split the sentence and get all word, no?
mark smith wrote:i'm lost
i tried a couple of solution on http://www.regexplanet.com/ but that alway fails.
Henry Wong wrote:
mark smith wrote:this code should split the sentence and get all word, no?
no
mark smith wrote:i'm lost
i tried a couple of solution on http://www.regexplanet.com/ but that alway fails.
It may be a good idea to start with a good tutorial on regular expressions. Regex is not something that can be learned by trail and error.
Henry
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:That regex looks pretty good to me. It's exactly what you want: words that contain 3 or more letters.
mark smith wrote:
Rob Spoor wrote:That regex looks pretty good to me. It's exactly what you want: words that contain 3 or more letters.
why when i check with : splitedMatcher.matches()
that return false?
mark smith wrote:that work, surely there is a better way to do it
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |