l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
November 4: Social gathering
Next Installfest:
TBD
Latest News:
Oct. 24: LUGOD election season has begun!
Page last updated:
2001 Dec 30 16:49

The following is an archive of a post made to our 'vox mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox] Help with Regular Expression?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox] Help with Regular Expression?



BTW, it is better to start a new thread if you are introducing a totally new
topic, rather than replying to something unrelated.

On Fri, Sep 28, 2001 at 11:24:37AM -0700, Richard S. Crawford wrote:
> How in the world would you go about putting a character at the beginning of 
> a string or the end if the character isn't there?
> 
> For example,
> 
>         Bob     becomes         ABobA
>         ABob    becomes         ABobA
>         BobA    becomes         ABobA
>         ABobA   becomes         ABobA

You want all the strings to begin and end with A, but not double the As if
the A is there already.

Let's say you are using Perl. One way is to look for a three-part string:

	/^(A)?(.*[^A])(A)?$/

You need the [^A] because regexps are greedy --- if you use .+ instead, you
will find that the string

	AfooA

matches as 1 = 'A', 2 = 'fooA', and 3 = '', because the second part of the
pattern has greedily matched the longest string it can get.

The replacement:

	/A$1A/

We just throw away substrings 1 and 3 because their values are unimportant;
I just used them to make the example easier to understand.

-- 
Henry House
OpenPGP key available from http://romana.hajhouse.org/hajhouse.asc

Attachment: pgp00008.pgp
Description: PGP signature



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Appahost Applications
For a significant contribution towards our projector, and a generous donation to allow us to continue meeting at the Davis Library.