• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex for Reversing Last Name, First Name?  RSS feed

 
James Dekker
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

Suppose I have a String which is similar to this:



How can I use a regex to turn it into:



Sincerely,

James
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The search string for would be something like "(.*), (.*)", and the replacement string would be something like "\\2 \\1". The exact syntax depends on the regex package you're using, but that's the general idea. You may have to escape the parentheses with backslashes, and probably want to make the search string more selective (and guard against several commas).
 
James Dekker
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

Thanks for replying!

I am fairly new to Java Regex. Am using the one which comes with JDK 1.5.

Don't really understand some of your tips, being a Java Regex newbie ...

This is what I have so far:



How does one suggest reversing "Smith, Joe" to "Joe Smith", and also what if someone puts a middle name, e.g.: "Smith, Joe E.", so it would become "Joe E. Smith"?

Sincerely,

James
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't actually have to use the regex engine directly... you could just use the string regex convenience methods. Try...



Henry
 
James Dekker
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry!

That explains it!

Cheers,

James
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!