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);