aspose file tools*
The moose likes Java in General and the fly likes need help creating a java regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "need help creating a java regex" Watch "need help creating a java regex" New topic
Author

need help creating a java regex

Yotam Ohayon
Greenhorn

Joined: Dec 31, 2010
Posts: 26
hi
i've been trying for some time now, but can't fully understand what i'm reading.
i want to create a regex pattern that will match any substring that starts with an underline '_' and ends with either a white space or end of line.

also, i've created this pattern "[A-Z]+" to look for words that contains ONLY capital letters. is this right?

thank you very much
YOtam
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I presume you have been through regular expressions tutorials and the API? You will find there are character classes which may match capital letters. In the Java™ Tutorials it tells you about line ends.
Yotam Ohayon
Greenhorn

Joined: Dec 31, 2010
Posts: 26
i have indeed. here's my text:


and here's the code i worte down:


finally the output is :


so i can locate the name (ie AFTER GREATER BEFORE) but can't locate the condition (ie date or size) as it keeps reading to the next name (GREATER)
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Yotam Ohayon wrote:

.......
so i can locate the name (ie AFTER GREATER BEFORE) but can't locate the condition (ie date or size) as it keeps reading to the next name (GREATER)

It goes on matching the remaining of the string because thats how the regex has been designed.
"_.*" pattern, matches any character that follows '_'.
So, if you want to say to the regex engine that match only non-space characters, you'll have to change the pattern a bit.
Just browse all the possible character classes and see if there is a character class that shall match any non-whitespace character.
If you find one, replace the '.' in your pattern with that, and see if you are able to extract all the conditions in the input text.


OCPJP 6
Yotam Ohayon
Greenhorn

Joined: Dec 31, 2010
Posts: 26
okay i've been studying regex for the past few hours and i'm getting there but not yet i have another problem now:
here's my text:



i want AFTER and GREATER, so i have the following regex:



at first its okay and i get AFTER, but the second time i get the entire line.
i tried add '?' before and after to turn it reluctant, also i tried \G to turn it global.
nothing works for me. in other times i used regex it has automatically started searching where last stopped, but not this time.
any suggestions?
thanks, Yotam
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

I tried you regex and it matched 'AFTER', 'GREATER' and 'NO'. I didnt get the entire line match though...
Can you post that code(-whatever you have tried) here, as that'd be very helpful to people when trying to figure out what went wrong.

I just added a word-boundary match character class to your regex, and it gives out 'AFTER' and 'GREATER' as expected.
Yotam Ohayon
Greenhorn

Joined: Dec 31, 2010
Posts: 26
of course... as you might have understood i am trying to create objects (filters) according to the text i read
AFTER and GREATER are filters, NOT is not a filter. here's the code:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help creating a java regex