473,748 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to match literal backslashes read from a text file using regular expressions?

I'm parsing a text file to extract word definitions. For example the
input text file contains the following content:

di.va.gate \'di_--v*-.ga_-t\ vb
pas.sim \'pas-*m\ adv : here and there : THROUGHOUT

I am trying to obtain words between two literal backslashes (\ .. \). I
am not able to match words between two literal backslashes using the
regxp - re.compile(r'\\[^\\]*\\').

Here is my sample script:

import re;

#slashPattern = re.compile(re.e scape(r'\\[^\\]*\\'));
pattern = r'\\[^\\]*\\'
slashPattern = re.compile(patt ern);

fdr = file( "parseinput",'r ');
line = fdr.readline();

while (line != ""):
if (slashPattern.m atch(line)):
print line.rstrip() + " <-- matches pattern " + pattern
else:
print line.rstrip() + " <-- DOES not match pattern " +
pattern
line = fdr.readline();
print;
----------
The output

C:\home\krishna \lang\python>py thon wsparsetest.py
python wsparsetest.py
di.va.gate \'di_--v*-.ga_-t\ vb <-- DOES not match
pattern \\[^\\]*\\
pas.sim \'pas-*m\ adv : here and there : THROUGHOUT <-- DOES not match
pattern \\[^\\]*\\
-----------

What should I be doing to match those literal backslashes?

Thanks

Jul 21 '05 #1
2 2137
cr*****@gmail.c om wrote:
I'm parsing a text file to extract word definitions. For example the
input text file contains the following content:

di.va.gate \'di_--v*-.ga_-t\ vb
pas.sim \'pas-*m\ adv : here and there : THROUGHOUT

I am trying to obtain words between two literal backslashes (\ .. \). I
am not able to match words between two literal backslashes using the
regxp - re.compile(r'\\[^\\]*\\').

Here is my sample script:

import re;
Lose the semicolons ...

#slashPattern = re.compile(re.e scape(r'\\[^\\]*\\'));
pattern = r'\\[^\\]*\\'
slashPattern = re.compile(patt ern);

fdr = file( "parseinput",'r ');
line = fdr.readline();

You should upgrade so that you have a modern Python and a modern
tutor[ial] -- then you will be writing:

for line in fdr:
do_something_wi th(line)

while (line != ""):
Lose the extraneous parentheses ...
if (slashPattern.m atch(line)):
Your main problem is that you should be using the search() method, not
the match() method. Read the section on this topic in the re docs!!
import re
pat = re.compile(r'\\[^\\]*\\')
pat.match(r'abc d \xyz\ pqr')
pat.search(r'ab cd \xyz\ pqr')

<_sre.SRE_Mat ch object at 0x00AE8988>

print line.rstrip() + " <-- matches pattern " + pattern
else:
print line.rstrip() + " <-- DOES not match pattern " +
pattern
line = fdr.readline();
print;
----------
The output

C:\home\krishna \lang\python>py thon wsparsetest.py
python wsparsetest.py
di.va.gate \'di_--v*-.ga_-t\ vb <-- DOES not match
pattern \\[^\\]*\\
pas.sim \'pas-*m\ adv : here and there : THROUGHOUT <-- DOES not match
pattern \\[^\\]*\\
-----------

What should I be doing to match those literal backslashes?

Thanks

Jul 21 '05 #2
This should give you an idea of how to go about it (needs python 2.3 or
newer):
import re
slashPattern = re.compile(r'\\ (.*?)\\')

for i,line in enumerate(file( "parseinput ")):
print "line", i+1,
match = slashPattern.se arch(line)
if match:
print "matched:", match.group(1)
else:
print "did not match"

#===== output =============== ========

line 1 matched: 'di_--v*-.ga_-t
line 2 matched: 'pas-*m

#============== =============== =======
George

Jul 21 '05 #3

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

Similar topics

3
2531
by: bdwise | last post by:
I have this in my body tag: something();something(); document.thisForm.textBox1.focus();something(); And I want to find a part between the semicolons that ends in focus() and remove the entire value between the semicolons. My Regular Expression looks like this but it is not matching, can anyone help?
0
1946
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match should be case-insensitive, but extract should have case preserved.
6
2324
by: Matt Wette | last post by:
Over the last few years I have converted from Perl and Scheme to Python. There one task that I do often that is really slick in Perl but escapes me in Python. I read in a text line from a file and check it against several regular expressions and do something once I find a match. For example, in perl ... if ($line =~ /struct {/) { do something } elsif ($line =~ /typedef struct {/) { do something else
1
2975
by: Alastair Cameron | last post by:
VB6, MSXML 3.2 installed: Q1. I am having a problem selecting nodes with XPATH expressions when an attribute values contain backslashes (\\) in as part of its value: For example the following statement fails to find a node (even though one exists in the XML) if the value of the LDAPServerURL attribute is \\LocalServer but works if the value is "LocalServer".
2
7957
by: Christian Staffe | last post by:
Hi, I would like to check for a partial match between an input string and a regular expression using the Regex class in .NET. By partial match, I mean that the input string could not yet be complete but I want to know if a match is possible so far. For instance I want to design a text box to enter a date and validate the correctness of the date as the user types character. If the user enters 1953/12/23 it will match my regex of course...
38
15964
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have something built-in that can accomplish this, or do I have to write my own parser? I don't want to reinvent the wheel if possible.
2
4752
by: brad | last post by:
Hello all, I'm new to javascript--not too new to a few other programming languages--and I need your help deciphering the Regexp in the following string. Regular expresions are hard enough in Python, and since I am new to javascript they are even harder. Well here's the string, thanks for any and all help I receive. document.URL.match(/^(.+?)(?:\?(?:(.*?)@)?(.+))?$/)
32
14696
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression engine doesn't exaust all the
14
4989
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before hand). The match string always starts with at least 5 digits.
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9552
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8245
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.