473,729 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a string problem

hi

if i have a some lines like this
a ) "here is first string"
b ) "here is string2"
c ) "here is string3"

When i specify i only want to print the lines that contains "string" ie
the first line and not the others. If i use re module, how to compile
the expression to do this? I tried the re module and using simple
search() and everytime it gives me all the 3 lines that have "string"
in it, whereas i only need line 1.
If re module is not needed, how can i use string manipulation to do
this? thanks

Jun 13 '06 #1
10 1769
mi*******@hotma il.com wrote:
hi

if i have a some lines like this
a ) "here is first string"
b ) "here is string2"
c ) "here is string3"

When i specify i only want to print the lines that contains "string" ie
the first line and not the others. If i use re module, how to compile
the expression to do this? I tried the re module and using simple
search() and everytime it gives me all the 3 lines that have "string"
in it, whereas i only need line 1.
If re module is not needed, how can i use string manipulation to do
this? thanks


As far as re goes, you can search for the pattern '\bstring\b', which
will find just the word 'string' itself. Not sure if there's a better
way to do it with REs.

And I'm actually ashamed to admit that I know the RE way, but not the
regular string manipulation way, if there is one! This seems like
something easy enough to do without REs though.
Jun 13 '06 #2

John Salerno wrote:
mi*******@hotma il.com wrote:
hi

if i have a some lines like this
a ) "here is first string"
b ) "here is string2"
c ) "here is string3"

When i specify i only want to print the lines that contains "string" ie
the first line and not the others. If i use re module, how to compile
the expression to do this? I tried the re module and using simple
search() and everytime it gives me all the 3 lines that have "string"
in it, whereas i only need line 1.
If re module is not needed, how can i use string manipulation to do
this? thanks


As far as re goes, you can search for the pattern '\bstring\b', which
will find just the word 'string' itself. Not sure if there's a better
way to do it with REs.

And I'm actually ashamed to admit that I know the RE way, but not the
regular string manipulation way, if there is one! This seems like
something easy enough to do without REs though.


thanks !

Jun 13 '06 #3
MTD
> When i specify i only want to print the lines that contains "string" ie
the first line and not the others. If i use re module, how to compile
the expression to do this? I tried the re module and using simple
search() and everytime it gives me all the 3 lines that have "string"
in it, whereas i only need line 1.


That's because all three lines DO include the substring "string"

Jun 13 '06 #4

John Salerno wrote:
mi*******@hotma il.com wrote:
hi

if i have a some lines like this
a ) "here is first string"
b ) "here is string2"
c ) "here is string3"

When i specify i only want to print the lines that contains "string" ie
the first line and not the others. If i use re module, how to compile
the expression to do this? I tried the re module and using simple
search() and everytime it gives me all the 3 lines that have "string"
in it, whereas i only need line 1.
If re module is not needed, how can i use string manipulation to do
this? thanks


As far as re goes, you can search for the pattern '\bstring\b', which
will find just the word 'string' itself. Not sure if there's a better
way to do it with REs.

And I'm actually ashamed to admit that I know the RE way, but not the
regular string manipulation way, if there is one! This seems like
something easy enough to do without REs though.


if RE has the \b and it works, can we look into the source of re and
see how its done for \b ?

Jun 13 '06 #5

John Salerno wrote:
mi*******@hotma il.com wrote:
hi

if i have a some lines like this
a ) "here is first string"
b ) "here is string2"
c ) "here is string3"

When i specify i only want to print the lines that contains "string" ie
the first line and not the others. If i use re module, how to compile
the expression to do this? I tried the re module and using simple
search() and everytime it gives me all the 3 lines that have "string"
in it, whereas i only need line 1.
If re module is not needed, how can i use string manipulation to do
this? thanks


As far as re goes, you can search for the pattern '\bstring\b', which
will find just the word 'string' itself. Not sure if there's a better
way to do it with REs.

And I'm actually ashamed to admit that I know the RE way, but not the
regular string manipulation way, if there is one! This seems like
something easy enough to do without REs though.


just curious , if RE has the \b and it works, can we look into the
source of re and see how its done for \b ?

Jun 13 '06 #6
mi*******@hotma il.com wrote:
just curious , if RE has the \b and it works, can we look into the
source of re and see how its done for \b ?


I had a look in the sre module (which re seems to import), but I
couldn't find much. I'm not the best at analyzing source code, though. :)

What is it you want to know about \b? It searches for the empty string
before and after a word (word being an alphanumeric character that can
include underscores).

A little more specific info is in the docs:

Matches the empty string, but only at the beginning or end of a word. A
word is defined as a sequence of alphanumeric or underscore characters,
so the end of a word is indicated by whitespace or a non-alphanumeric,
non-underscore character. Note that \b is defined as the boundary
between \w and \ W, so the precise set of characters deemed to be
alphanumeric depends on the values of the UNICODE and LOCALE flags.
Inside a character range, \b represents the backspace character, for
compatibility with Python's string literals.
Jun 13 '06 #7

John Salerno wrote:
mi*******@hotma il.com wrote:
just curious , if RE has the \b and it works, can we look into the
source of re and see how its done for \b ?


I had a look in the sre module (which re seems to import), but I
couldn't find much. I'm not the best at analyzing source code, though. :)

What is it you want to know about \b? It searches for the empty string
before and after a word (word being an alphanumeric character that can
include underscores).

A little more specific info is in the docs:

Matches the empty string, but only at the beginning or end of a word. A
word is defined as a sequence of alphanumeric or underscore characters,
so the end of a word is indicated by whitespace or a non-alphanumeric,
non-underscore character. Note that \b is defined as the boundary
between \w and \ W, so the precise set of characters deemed to be
alphanumeric depends on the values of the UNICODE and LOCALE flags.
Inside a character range, \b represents the backspace character, for
compatibility with Python's string literals.


thanks..actuall y i had seen \b in the docs before, just that it slipped
my mind when i was doing the coding. was even meddling with look aheads
...which is not the answer anyway.
well, since re has the \b, was wondering why there is no implementation
of it in strings. So the idea of looking at the source or re on how
it's done came to my mine..i suppose we have to go down to viewing the
C source then..:-)

Jun 13 '06 #8

mi*******@hotma il.com wrote:
hi

if i have a some lines like this
a ) "here is first string"
b ) "here is string2"
c ) "here is string3"

When i specify i only want to print the lines that contains "string" ie
the first line and not the others. If i use re module, how to compile
the expression to do this? I tried the re module and using simple
search() and everytime it gives me all the 3 lines that have "string"
in it, whereas i only need line 1.
If re module is not needed, how can i use string manipulation to do
this? thanks


If this is a RL-situation,
if mystring.endswi th('string') will do

Jun 13 '06 #9
Le Mardi 13 Juin 2006 15:59, John Salerno a écrit*:
And I'm actually ashamed to admit that I know the RE way, but not the
regular string manipulation way, if there is one!
eheh,

In [39]: import string

In [40]: sub, s1, s2 = 'string', 're string2, ,string1', 're string2, ,string'

In [41]: sub in [ e.strip(string. punctuation) for e in s1.split() ]
Out[41]: False

In [42]: sub in [ e.strip(string. punctuation) for e in s2.split() ]
Out[42]: True
This seems like
something easy enough to do without REs though.


Yes, but python way seems a little faster

python2.4 -mtimeit -s "import re" "re.match('\bst ring\b', 're
string2, ,string1') and True"
100000 loops, best of 3: 7.3 usec per loop
python2.4 -mtimeit -s "import string" "'string' in [
e.strip(string. punctuation) for e in 're string2, ,string1'.split () ]"
100000 loops, best of 3: 6.99 usec per loop
--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
Jun 13 '06 #10

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

Similar topics

7
10627
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using namespace std; : : vector<string>* getTyphoon()
17
14351
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart, int longueur) { char *resultat = " "; char *temporaire = " "; int nbr;
51
8279
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct code? many thx!
18
7191
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1 QID=\"55111\"><Tag2 AID=\"5511101\"></Tag2></Tag1><Tag1 QID=\"55112\"><Tag2 AID=\"5511217\"></Tag2></Tag1><Tag1 QID=\"5512282\"><Tag2 AID=\"551228206\"></Tag2></Tag1><Tag1 QID=\"55114\"><Tag2 AID=\"5511406\"></Tag2></Tag1><Tag1 QID=\"55115\"><Tag2
32
14878
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
12
9637
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34) & ", " & Chr(34) & CurrentEventDetails & ")" strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName & "</A><BR>" The problem I have is that when the string variables or contain a string with an...
4
5276
by: MooMaster | last post by:
After some google searching on the forum I couldn't find any topics that seemed to relate exactly to my problem, so hopefully someone can help me out... I'm running python 2.4.1 on a local Win2K system and doing some work-related development on an AIX box, and I'm running into a problem using cx_Oracle to add some information to an Oracle 9.2 table. I have a table_title defined as a VARCHAR2 data type, and when I'm trying to retrieve...
6
2205
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
5
3534
by: ThatVBGuy | last post by:
Hello All, I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and find and replace some tags. I have 3 functions. 1 to open the doc, the 2nd to find and replace the tags the 3rd to save the info. the code is pasted below : Public Function ReadFileContents(FileFullPath As String) As _ String On Error GoTo...
1
8374
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As you can see, there could have been a problem with the split-method. The following short article handles ways around this possible problem, that we couldn't reproduce, but someone may possibly encounter it too sometimes. If nothing else, the shown...
0
8917
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
9426
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
9281
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
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
8148
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
6722
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
4525
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...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2163
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.