This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular expression to take last word in a string

 
Raja Mirrah
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a requirement wherein i have to take the last word which in coming in a string.

Ex:

I have a string in below format

String Str = "2009-08-05T15:07:12-04:00#2009-08-05 15:07:09 EDT: Failed or Error in Loopback for OCU"

I have to take the word OCU from the given string

Note: I donot know what string is coming as input and only thing i know is i have to take the last word in the string in this case the last word is OCU.

I am trying to use the regular expression in replaceFirst method.


Can anyone give me regex which will satisfy my requirement?

Early response is highly appreciated!!!

Warm Regards,
Raj
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming the word separator is space " " (as it usually is). The following code works fine:

 
Antany Vasanth
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Raja,

Regular expression is very powerfull tool to handle various string manipulation/parsing etc. Use it when its really required.

You can simply do it using some basic String methods.



The above code just finds the last space and then get the remaining string.

Incase if you want to use regular expression use the following code to match the last string


The above regular expression also uses the same logic to findout the last word. It just looks for the space followed by any word and that should be last one.

Regards,
Antany
 
Raja Mirrah
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Antany,

Even i have done it using String Function as you said using lastIndex.

But Since regex is powerful, i thought of doing it . Anyways Thank you so much.....
 
Roldan Baldo
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it can be done also like this:



you can try many different ways. have fun.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Roldan Baldo. Please use the CODE button; I have edited your post so you can see how much better it looks.
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite efficient, Roldan. Apart from the compiler error when comparing a char to a String (you should use '' instead of ""), multiple String concatenations should be prevented (single line String concatenations are ok). Use a StringBuilder instead. It also has a built-in reverse operator:
Now ideally you wouldn't replace but insert the char at position 0, but that can be quite bad for performance if the last word is long - it needs to shift elements in its internal array all the time.
 
anandhi mohan
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try This,

 
Roldan Baldo
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow! thanks rob and campbell.

i thought it is ok to compare string in my post because there is no compile error. thanks to the comment in my code, now the code is more specific.

i just want to be part of javaranch.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raja Mirrah wrote:
String Str = "2009-08-05T15:07:12-04:00#2009-08-05 15:07:09 EDT: Failed or Error in Loopback for OCU"

I have to take the word OCU from the given string

...

I am trying to use the regular expression in replaceFirst method.


Since the OP requested that the solution use the replaceFirst() method, here it is...



Henry
 
Raja Mirrah
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,

Even i have written the same regex which you have posted. thanks!!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic