Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Regular Expressions

Thread view: 
jack.smith.sam@gmail.com - 07 Oct 2006 13:54 GMT
Hi All,

I have a string which can be in these two formats : "<b>word" or "word"
(<b> may or may not be present)
I used the following regular expression to extract word (word is
similar to email ID and may containt letter,-,_):

[<b>]*([\\w-_]+)

but for some words like "best' it outputs "est" and removes the "b". I
think [<b>] means < or b or >.
Can you help me solving this issue(preferably without using additional
parentheses)?
Thanks a lot.
Pielmeier Markus - 07 Oct 2006 14:03 GMT
Hi Jack,

On Sat, 07 Oct 2006 05:54:17 -0700, jack.smith.sam wrote:
> I have a string which can be in these two formats : "<b>word" or "word"
> (<b> may or may not be present)
> I used the following regular expression to extract word (word is
> similar to email ID and may containt letter,-,_):
>
> [<b>]*([\\w-_]+)
Try this: "(?:<b>)?([\\w-_]+)"

> but for some words like "best' it outputs "est" and removes the "b". I
> think [<b>] means < or b or >.
The problem is, that the [] parenthesis treats the characters not as
string, but as individual characters which can appear individially.

Greetings,
Markus
Martin Gerner - 07 Nov 2006 22:55 GMT
jack.smith.sam@gmail.com wrote in news:1160225657.635244.130570
@m7g2000cwm.googlegroups.com:

> I have a string which can be in these two formats : "<b>word" or "word"
> (<b> may or may not be present)
[quoted text clipped - 7 lines]
> Can you help me solving this issue(preferably without using additional
> parentheses)?

Another, perhaps simpler, alternative is of course to solve it with indexOf
and substring. It takes a bit more space in your code, though.

String extractedString = null;
if (myString.indexOf("<b>") == 0)
       extractedString = myString.substring(3);
else
       extractedString = myString;

Signature

Martin Gerner

Lew - 12 Nov 2006 16:24 GMT
> Another, perhaps simpler, alternative is of course to solve it with indexOf
> and substring. It takes a bit more space in your code, though.
[quoted text clipped - 4 lines]
> else
>         extractedString = myString;

Why the "throwaway" initialization of extractedString = null?

- Lew
Lars Enderin - 12 Nov 2006 17:23 GMT
Lew skrev:
>> Another, perhaps simpler, alternative is of course to solve it with
>> indexOf and substring. It takes a bit more space in your code, though.
[quoted text clipped - 5 lines]
>
> Why the "throwaway" initialization of extractedString = null?

I prefer

String extractedString = (myString.indexOf("<b>") == 0 ?
                          myString.substring(3) : myString);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.