Quickie - Regexp for a string not at the beginning of the line

MRAB python at mrabarnett.plus.com
Thu Oct 25 22:11:48 EDT 2012


On 2012-10-26 02:08, Rivka Miller wrote:
> On Oct 25, 2:27 pm, Danny <dann90... at gmail.com> wrote:
>> Why you just don't give us the string/input, say a line or two, and what you want off of it, so we can tell better what to suggest
>
> no one has really helped yet.
>
> I want to search and modify.
>
> I dont wanna be tied to a specific language etc so I just want a
> regexp and as many versions as possible. Maybe I should try in emacs
> and so I am now posting to emacs groups also, although javascript has
> rich set of regexp facilities.
>
> examples
>
> $hello$ should not be selected but
> not hello but all of the $hello$ and $hello$ ... $hello$ each one
> selected
>
[snip]
To match the literal "$hello$" except at the start of a line, use:

(?<!^)\$hello\$

with the multiline flag set. You could set the multiline flag within
the regex like this:

(?m)(?<!^)\$hello\$

re.search will find the first occurrence. In order to find all such
occurrences in Python, you need to use re.findall or re.finditer.
(Other languages have their own ways.)

Note that there are different 'flavours' of regex, the most common form
following the lead of Perl, and that implementations might differ in
which features they support.



More information about the Python-list mailing list