| Author |
Regular expression to take last word in a string
|
Raja Mirrah
Greenhorn
Joined: Jun 23, 2009
Posts: 26
|
|
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
|
Warm Regards,
Raj
|
 |
Gerardo Tasistro
Ranch Hand
Joined: Feb 08, 2005
Posts: 362
|
|
Assuming the word separator is space " " (as it usually is). The following code works fine:
|
 |
Antany Vasanth
Ranch Hand
Joined: Jan 28, 2009
Posts: 43
|
|
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
Joined: Jun 23, 2009
Posts: 26
|
|
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
Joined: Aug 11, 2009
Posts: 99
|
|
it can be done also like this:
you can try many different ways. have fun.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
anandhi mohan
Greenhorn
Joined: Mar 24, 2009
Posts: 9
|
|
Try This,
|
 |
Roldan Baldo
Ranch Hand
Joined: Aug 11, 2009
Posts: 99
|
|
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
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Raja Mirrah
Greenhorn
Joined: Jun 23, 2009
Posts: 26
|
|
Thanks Henry,
Even i have written the same regex which you have posted. thanks!!!
|
 |
 |
|
|
subject: Regular expression to take last word in a string
|
|
|