• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Regex for capital letters

 
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply but as I said I need the regex
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general answers are not simply handed out here--what have you come up with so far?
 
Marshal
Posts: 80111
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic