Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex for capital letters

 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for the reply but as I said I need the regex
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general answers are not simply handed out here--what have you come up with so far?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic