File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Regex for capital letters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regex for capital letters" Watch "Regex for capital letters" New topic
Author

Regex for capital letters

Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

Hi,
How can I get the index of 4th capital letter in a string with using pattern and matcher ?
Actually I need the regex.
Thanks for any help.
Greetings
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

There are (at least) two ways for this:
1) simply look for one capital letter, then use Matcher.find() 4 times
2) use a regular expression that immediately finds the 4th capital letter. You will most likely need a capturing group for that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

Thanks for the reply but as I said I need the regex
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In general answers are not simply handed out here--what have you come up with so far?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
There are all sorts of details in the Java™ Tutorials. We don't simply give answers like that; we expect you to do the work and we'll have a look at it.
Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

I have following code but i am not sure if there is a better way especially with better regex so I do not need variable i and the loop.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18973
    
  40

2) use a regular expression that immediately finds the 4th capital letter. You will most likely need a capturing group for that.


Of course, as soon as I read this... I wondered if it was possible to do this without a capturing group. And thought of two ways....

1. You can use a zero length look-behind for the beginning and the first three capital letters -- then you can take the start() of the match. (look behinds are non-capturing).

2. You can do the regex that makes sures that it matches the 4th capital letter as the last part of the match, and then use end() minus one on the match.

But I agree. Using a capturing group is probably easiest.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18973
    
  40

I am actually a bit rusty with character classes, but I think that this....



Is the same as this....



So, basically, you are trying to match a single character, that is either a capital letter or a period.

Anyway, your solution is as Rob describes...

1) simply look for one capital letter, then use Matcher.find() 4 times


You just need to get rid of the period, as you don't want to match that.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex for capital letters