Regular Expressions

J

jack.smith.sam

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.
 
P

Pielmeier Markus

Hi Jack,

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 >.
The problem is, that the [] parenthesis treats the characters not as
string, but as individual characters which can appear individially.


Greetings,
Markus
 
M

Martin Gerner

(e-mail address removed) wrote in @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)
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)?

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

Lew

Martin said:
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;

Why the "throwaway" initialization of extractedString = null?

- Lew
 
L

Lars Enderin

Lew skrev:
Why the "throwaway" initialization of extractedString = null?

I prefer

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top