Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regular Expressions

Reply
Thread Tools

Regular Expressions

 
 
jack.smith.sam@gmail.com
Guest
Posts: n/a
 
      10-07-2006
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.

 
Reply With Quote
 
 
 
 
Pielmeier Markus
Guest
Posts: n/a
 
      10-07-2006
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
 
Reply With Quote
 
 
 
 
Martin Gerner
Guest
Posts: n/a
 
      11-07-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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)
> 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;

--
Martin Gerner
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-12-2006
Martin Gerner wrote:
> 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
 
Reply With Quote
 
Lars Enderin
Guest
Posts: n/a
 
      11-12-2006
Lew skrev:
> Martin Gerner wrote:
>> 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?


I prefer

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Regular Expressions in ASP.net Jay Douglas ASP .Net 3 11-03-2003 08:09 PM
Regular expressions mark Perl 4 10-28-2003 12:37 PM
perl regular expressions return last matched occurence? Dustin D. Perl 1 08-28-2003 01:51 AM
matching curly braces and regular expressions Dustin D. Perl 0 08-26-2003 11:18 PM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments