wood burning stoves 2.0*
The moose likes Java in General and the fly likes Regular expression help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular expression help needed" Watch "Regular expression help needed" New topic
Author

Regular expression help needed

Prashant Langade
Ranch Hand

Joined: Jul 01, 2009
Posts: 71
Hi

I dont have much knowledge of Regular expressions.

My requirement is to match the string starting and ending with underscore i.e "_"

that means

if i have strings as
1> _abc_
2>_abc_av
3>ab_abc_
4>_pqr_

then only _abc_ and _pqr should be able to match the pattern.
Can any one please help me?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41107
    
  45
Something like "^_.*_$" should do the trick.


Ping & DNS - my free Android networking tools app
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
Ulf Dittmer wrote:Something like "^_.*_$" should do the trick.


"^_.*_$" will also match __ (two underscores, with no string). If you don't want that then just replace the *(which means zero or more) with +(which means at least 1 or more). "^_.+_$"
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regular expression help needed
 
Similar Threads
javascript event ?
Using contains/equals/== with random characters?
Regular expression to search for words not beginning with a single quote and with or without spaces
Multiple file output with JDK 6 XSLT implementation
HibernateTools Generating Wrong Code