The primary question is how do I perform a match when the regular
expression contains string variables? For example, in the following
code I want to match a line that starts with STX, then has any number
of characters, then ends with STX.
Example 2 I'm pretty sure works as I expect, but I'm not sure about
Example 1, and I'm pretty sure about example 3.
import re
from curses.ascii import STX,ETX,FS
STX = chr(STX)
ETX = chr(ETX)
FS = chr(FS)
data = STX + "ONE" + FS + "TWO" + FS + "THREE" + ETX
match = STX + '(.*)' + ETX
# Example 1
# This appears to work, but I'm not sure if the '+' is being used in
the regular expression, or if it's just joining STX, '(.*)', and ETX.
if re.search(STX + '(.*)' + ETX,data):
print "Matches"
# Example 2
# This also appears to work
if re.search(match,data):
print "Matches"
# Example 3
# Doesn't work, as STX and ETX are evaluated as the literal strings
'STX' and 'ETX'
if re.search('STX(.*)ETX', data):
print "Matches"
Chris 3 2488
snacktime a écrit : The primary question is how do I perform a match when the regular expression contains string variables? For example, in the following code I want to match a line that starts with STX, then has any number of characters, then ends with STX. Example 2 I'm pretty sure works as I expect, but I'm not sure about Example 1, and I'm pretty sure about example 3.
import re from curses.ascii import STX,ETX,FS STX = chr(STX) ETX = chr(ETX) FS = chr(FS) data = STX + "ONE" + FS + "TWO" + FS + "THREE" + ETX match = STX + '(.*)' + ETX
# Example 1 # This appears to work, but I'm not sure if the '+' is being used in the regular expression, or if it's just joining STX, '(.*)', and ETX.
if re.search(STX + '(.*)' + ETX,data): print "Matches"
# Example 2 # This also appears to work if re.search(match,data): print "Matches"
# Example 3 # Doesn't work, as STX and ETX are evaluated as the literal strings 'STX' and 'ETX' if re.search('STX(.*)ETX', data): print "Matches"
You may want something like:
if re.search('%s(.*)%s' % (STX, ETX), data):
...
BTW, given your requirements, I'd write this:
if re.search('^%s(.*)%s$' % (STX, ETX), data):
...
Chris
> You may want something like: if re.search('%s(.*)%s' % (STX, ETX), data):
Ah I didn't even think about that...
Chris
Bruno Desthuilliers wrote: match = STX + '(.*)' + ETX
# Example 1 # This appears to work, but I'm not sure if the '+' is being used in the regular expression, or if it's just joining STX, '(.*)', and ETX.
if re.search(STX + '(.*)' + ETX,data): print "Matches"
# Example 2 # This also appears to work if re.search(match,data): print "Matches"
You may want something like: if re.search('%s(.*)%s' % (STX, ETX), data): ...
that's of course the same thing as examples 1 and 2.
a tip to the original poster: if you're not sure what an expression does,
try printing the result. use "print repr(v)" if the value may contain odd
characters. try adding this to your test script:
print repr(match)
print repr(STX + '(.*)' + ETX)
print repr('%s(.*)%s' % (STX, ETX))
</F> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Vibha Tripathi |
last post by:
Hi Folks,
I put a Regular Expression question on this list a
couple days ago. I would like to rephrase my question
as below:
In the Python re.sub(regex, replacement, subject)...
|
by: Bradley Plett |
last post by:
I'm hopeless at regular expressions (I just don't use them often
enough to gain/maintain knowledge), but I need one now and am looking
for help. I need to parse through a document to find a URL,...
|
by: Lee Kuhn |
last post by:
I am trying the create a regular expression that will essentially match
characters in the middle of a fixed-length string. The string may be any
characters, but will always be the same length. In...
|
by: Q. John Chen |
last post by:
I have Vidation Controls
First One: Simple exluce certain special characters:
say no a or b or c in the string:
*
Second One:
I required date be entered in "MM/DD/YYYY" format:
//+4 How...
|
by: Ryan |
last post by:
HELLO
I am using the following MICROSOFT SUGGESTED (somewhere on msdn)
regular expression to validate email addresses however I understand
that the RFP allows for "+" symbols in the email address...
|
by: norton |
last post by:
Hello,
Does any one know how to extact the following text into 4 different
groups(namely Date, Artist, Album and Quality)?
- Artist - Album
Artist - Album
- Artist - Album
- Artist -...
|
by: Billa |
last post by:
Hi,
I am replaceing a big string using different regular expressions (see
some example at the end of the message). The problem is whenever I
apply a "replace" it makes a new copy of string and I...
|
by: Ludwig |
last post by:
Hi,
i'm using the regular expression \b\w to find the beginning of a word,
in my C# application. If the word is 'public', for example, it works.
However, if the word is '<public', it does not...
|
by: Zach |
last post by:
Hello,
Please forgive if this is not the most appropriate newsgroup for this
question. Unfortunately I didn't find a newsgroup specific to regular
expressions.
I have the following regular...
|
by: Mike |
last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in
matches. I would like to get what the actual regular expression is.
In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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: 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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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,...
| |