473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

regex module, or don't work as expected

Howdy,
i have the following regex "iface lo[\w\t\n\s]+(?=(iface)|$)"

If "iface" don't follow after the regex "iface lo[\w\t\n\s]" the rest of
the text should be selected.
But ?=(iface) is ignored, it is always the whole texte selected.
What is wrong?
many thanks

greetings

Fabian
Jul 4 '06 #1
3 1094
In <44**********************@read.cnntp.org>, Fabian Holler wrote:
Howdy,
i have the following regex "iface lo[\w\t\n\s]+(?=(iface)|$)"

If "iface" don't follow after the regex "iface lo[\w\t\n\s]" the rest of
the text should be selected.
But ?=(iface) is ignored, it is always the whole texte selected.
What is wrong?
The ``+`` after the character class means at least one of the characters
in the class or more. If you have a text like:

iface lox iface

Then the it matches the space and the word ``iface`` because the space
(``\s``) and word characters (``\w``) are part of the character class and
``+`` is "greedy". It consumes as many characters as possible and the
rest of the regex is only evaluated when there are no matches anymore.

If you want to match non-greedy then put a ``?`` after the ``+``::

iface lo[\w\t\n\s]+?(?=(iface)|$)

Now only "iface lox " is matched in the example above.

Ciao,
Marc 'BlackJack' Rintsch
Jul 4 '06 #2
Hello Marc,

thank you for your answer.

Marc 'BlackJack' Rintsch wrote:
In <44**********************@read.cnntp.org>, Fabian Holler wrote:
>i have the following regex "iface lo[\w\t\n\s]+(?=(iface)|$)"

If "iface" don't follow after the regex "iface lo[\w\t\n\s]" the rest of
the text should be selected.
But ?=(iface) is ignored, it is always the whole texte selected.
What is wrong?

The ``+`` after the character class means at least one of the characters
in the class or more. If you have a text like:
Yes thats right, but that isn't my problem.
The problem is in the "(?=(iface)|$)" part.

I have i.e. the text:

"auto lo eth0
<MATCH START>iface lo inet loopback
bla
blub

<MATCH END>iface eth0 inet dhcp
hostname debian"
My regex should match the marked text.
But it matchs the whole text starting from iface.
If there is only one iface entry, the whole text starting from iface
should be matched.

greetings

Fabian
Jul 4 '06 #3
Fabian Holler wrote:
Yes thats right, but that isn't my problem.
The problem is in the "(?=(iface)|$)" part.
no, the problem is that you're thinking "procedural string matching from
left to right", but that's not how regular expressions work.
I have i.e. the text:

"auto lo eth0
<MATCH START>iface lo inet loopback
bla
blub

<MATCH END>iface eth0 inet dhcp
hostname debian"
My regex should match the marked text.
But it matchs the whole text starting from iface.
which is perfectly valid, since a plain "+" is greedy, and you've asked
for "iface lo" followed by some text followed by *either* end of string
or another "iface". the rest of the string is a perfectly valid string.

if you want a non-greedy match, use "+?" instead.

however, if you just want the text between two string literals, it's
often more efficient to just split the string twice:

text = text.split("iface lo", 1)[1].split("iface", 1)[0]

</F>

Jul 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
2
by: Sriram | last post by:
Hi, I am having trouble matching a regex that combines a negated character class and an anchor ($). Basically, I want to match all strings that don't end in a digit. So I tried: ...
2
by: John Perks and Sarah Mount | last post by:
I have to split some identifiers that are casedLikeThis into their component words. In this instance I can safely use to represent uppercase, but what pattern should I use if I wanted it to work...
7
by: Dennis Gearon | last post by:
If I've read everything right, in order to get: multiple languages on a site with the functionality of ALL of: REGEX LIKE Correctly sorted text
3
by: gisleyt | last post by:
I'm trying to compile a perfectly valid regex, but get the error message: r = re.compile(r'(*)(\d{1,3}\.\d{0,2})?(\d*)(\,\d{1,3}\.\d{0,2})?(\d*)?.*') Traceback (most recent call last): File...
7
by: Jordi Rico | last post by:
Hi, I know I can split a string into an array doing this: Dim s As String()=Regex.Split("One-Two-Three","-") So I would have: s(0)="One" s(1)="Two"
11
by: proctor | last post by:
hello, i have a regex: rx_test = re.compile('/x()*x/') which is part of this test program: ============ import re
1
by: TtfnJohn | last post by:
I have two small scripts that while on the surface should both work the problem is they don't. Here's the first one: import re testString = 'Thap,fpvi,*!wtyd@*.dip.t-dialin.net:*!ylx@*.dip.t-...
0
by: MRAB | last post by:
I'm working on the sources for the regex module (_sre.c) but I've come across some behaviour that I wasn't aware of before: ('b', 'a') The regex module was modified to return this instead of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.