Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Quickie - Regexp for a string not at the beginning of the line (http://www.velocityreviews.com/forums/t953916-quickie-regexp-for-a-string-not-at-the-beginning-of-the-line.html)

Rivka Miller 10-25-2012 08:53 PM

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

I am looking for a regexp for a string not at the beginning of the
line.

For example, I want to find $hello$ that does not occur at the
beginning of the string, ie all $hello$ that exclude ^$hello$.

In addition, if you have a more difficult problem along the same
lines, I would appreciate it. For a single character, eg < not at the
beginning of the line, it is easier, ie

^[^<]+<

but I cant use the same method for more than one character string as
permutation is present and probably for more than one occurrence,
greedy or non-greedy version of [^<]+ would pick first or last but not
the middle ones, unless I break the line as I go and use the non-
greedy version of +. I do have the non-greedy version available, but
what if I didnt?

If you cannot solve the problem completely, just give me a quick
solution with the first non beginning of the line and I will go from
there as I need it in a hurry.

Thanks



Janis Papanagnou 10-25-2012 09:01 PM

Re: Quickie - Regexp for a string not at the beginning of the line
 
On 25.10.2012 22:53, Rivka Miller wrote:
> Hello Programmers,
>
> I am looking for a regexp for a string not at the beginning of the
> line.
>
> For example, I want to find $hello$ that does not occur at the
> beginning of the string, ie all $hello$ that exclude ^$hello$.


.hello

The dot represents any character. But for specific strings that
needs adjustments (e.g. looking for hh not at the beginning of a
line would require something like ^[^h]+hh - ah, well, you wrote
something similar below).

Janis

>
> In addition, if you have a more difficult problem along the same
> lines, I would appreciate it. For a single character, eg < not at the
> beginning of the line, it is easier, ie
>
> ^[^<]+<
>
> but I cant use the same method for more than one character string as
> permutation is present and probably for more than one occurrence,
> greedy or non-greedy version of [^<]+ would pick first or last but not
> the middle ones, unless I break the line as I go and use the non-
> greedy version of +. I do have the non-greedy version available, but
> what if I didnt?
>
> If you cannot solve the problem completely, just give me a quick
> solution with the first non beginning of the line and I will go from
> there as I need it in a hurry.
>
> Thanks
>
>



Zero Piraeus 10-25-2012 09:21 PM

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

On 25 October 2012 16:53, Rivka Miller <rivkaumiller@gmail.com> wrote:
> I am looking for a regexp for a string not at the beginning of the
> line.


There are probably quite a few ways to do this, but '(?<!^)PATTERN'
has the advantage of explicitly describing what you're trying to do.
For instance:

>>> pattern = re.compile(r"(?<!^)\b\w+\b")
>>> re.findall(pattern, "this is some text")

['is', 'some', 'text']

-[]z.

Rivka Miller 10-26-2012 01:08 AM

Re: Quickie - Regexp for a string not at the beginning of the line
 
On Oct 25, 2:27*pm, Danny <dann90...@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

=================
original post
=================


Hello Programmers,

I am looking for a regexp for a string not at the beginning of the
line.

For example, I want to find $hello$ that does not occur at the
beginning of the string, ie all $hello$ that exclude ^$hello$.

In addition, if you have a more difficult problem along the same
lines, I would appreciate it. For a single character, eg < not at the
beginning of the line, it is easier, ie

^[^<]+<

but I cant use the same method for more than one character string as
permutation is present and probably for more than one occurrence,
greedy or non-greedy version of [^<]+ would pick first or last but not
the middle ones, unless I break the line as I go and use the non-
greedy version of +. I do have the non-greedy version available, but
what if I didnt?

If you cannot solve the problem completely, just give me a quick
solution with the first non beginning of the line and I will go from
there as I need it in a hurry.

Thanks


Dave Angel 10-26-2012 01:21 AM

Re: Quickie - Regexp for a string not at the beginning of the line
 
On 10/25/2012 09:08 PM, Rivka Miller wrote:
> On Oct 25, 2:27 pm, Danny <dann90...@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.
>
> <SNIP>
>
> first non beginning of the line and I will go from
> there as I need it in a hurry.
>
>


Call a tow truck and tell him to jump your spare tire from his left turn
signal. That'll be about as effective. But crying wolf to several
towns at once is probably a mistake.

--

DaveA


Ed Morton 10-26-2012 02:00 AM

Re: Quickie - Regexp for a string not at the beginning of the line
 
On 10/25/2012 8:08 PM, Rivka Miller wrote:
> On Oct 25, 2:27 pm, Danny <dann90...@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.


Because there is no solution - there IS no _RE_ that will match a string not at
the beginning of a line.

Now if you want to know how to extract a string that matches an RE in awk,
that'd be (just one way):

awk 'match($0,/.[$]hello[$]/) { print substr($0,RSTART+1,RLENGTH-1) }'

and other tools would have their ways of producing the same output, but that's
not the question you're asking.

Ed.
>
> 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
>
> =================
> original post
> =================
>
>
> Hello Programmers,
>
> I am looking for a regexp for a string not at the beginning of the
> line.
>
> For example, I want to find $hello$ that does not occur at the
> beginning of the string, ie all $hello$ that exclude ^$hello$.
>
> In addition, if you have a more difficult problem along the same
> lines, I would appreciate it. For a single character, eg < not at the
> beginning of the line, it is easier, ie
>
> ^[^<]+<
>
> but I cant use the same method for more than one character string as
> permutation is present and probably for more than one occurrence,
> greedy or non-greedy version of [^<]+ would pick first or last but not
> the middle ones, unless I break the line as I go and use the non-
> greedy version of +. I do have the non-greedy version available, but
> what if I didnt?
>
> If you cannot solve the problem completely, just give me a quick
> solution with the first non beginning of the line and I will go from
> there as I need it in a hurry.
>
> Thanks
>



Ben Bacarisse 10-26-2012 02:11 AM

Re: Quickie - Regexp for a string not at the beginning of the line
 
Rivka Miller <rivkaumiller@gmail.com> writes:

> On Oct 25, 2:27Â*pm, Danny <dann90...@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.


Really? I was going to reply but then I saw Janis had given you the
answer. If it's not the answer, you should just reply saying what it is
that's wrong with it.

> I want to search and modify.


Ah. That was missing from the original post. You can't expect people
to help with questions that weren't asked! To replace you will usually
have to capture the single preceding character. E.g. in sed:

sed -e 's/\(.\)$hello\$/\1XXX/'

but some RE engines (Perl's, for example) allow you specify zero-width
assertions. You could, in Perl, write

s/(?<=.)\$hello\$/XXX/

without having to capture whatever preceded the target string. But
since Perl also has negative zero-width look-behind you can code your
request even more directly:

s/(?<!^)\$hello\$/XXX/

> 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.


You can't always have a universal solution because different PE
implementations have different syntax and semantics, but you should be
able to translate Janis's solution of matching *something* before your
target into every RE implementation around.

> examples
>
> $hello$ should not be selected but
> not hello but all of the $hello$ and $hello$ ... $hello$ each one
> selected


I have taken your $s to be literal. That's not 100 obvious since $ is a
common (universal?) RE meta-character.

<snip>
--
Ben.

MRAB 10-26-2012 02:11 AM

Re: Quickie - Regexp for a string not at the beginning of the line
 
On 2012-10-26 02:08, Rivka Miller wrote:
> On Oct 25, 2:27 pm, Danny <dann90...@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.

Mark Lawrence 10-26-2012 02:37 AM

Re: Quickie - Regexp for a string not at the beginning of the line
 
On 25/10/2012 21:53, Rivka Miller wrote:
> Hello Programmers,
>
> I am looking for a regexp for a string not at the beginning of the
> line.
>


Why bother with a silly regex thingy when simple string methods will
suffice e.g.

'yourstring'.find('xyz', 1)

or

'yourstring'.index('xyz', 1)

or

'xyz' in 'yourstring'[1:]

--
Cheers.

Mark Lawrence.


anon@anon.anon 10-26-2012 03:22 AM

Re: Quickie - Regexp for a string not at the beginning of the line
 
On Thu, 25 Oct 2012 18:08:53 -0700 (PDT), Rivka Miller
<rivkaumiller@gmail.com> wrote in
<73f60cf3-d932-4366-a405-6767488560c6@q16g2000yqc.googlegroups.com>:

>no one has really helped yet.


We regret that you are not a satisfied customer.

Please take your receipt to the cashier and you will receive double your
money back according to our "you must be satisfied" guarantee.


All times are GMT. The time now is 02:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57