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
|