• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Regular expression to take last word in a string

 
Greenhorn
Posts: 26
  • 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
 
Ranch Hand
Posts: 362
  • 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:

 
Ranch Hand
Posts: 44
  • 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
  • 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.....
 
Ranch Hand
Posts: 99
  • 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.
 
Marshal
Posts: 66179
250
  • 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.
 
Sheriff
Posts: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • 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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try This,

 
Roldan Baldo
Ranch Hand
Posts: 99
  • 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.
 
author
Posts: 23843
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • 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
  • 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!!!
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!