473,385 Members | 1,630 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.

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 1749
mi*******@hotmail.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*******@hotmail.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*******@hotmail.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*******@hotmail.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*******@hotmail.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*******@hotmail.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..actually 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*******@hotmail.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.endswith('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('\bstring\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
John Salerno wrote:
mi*******@hotmail.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

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


I'd just split it on whitespace, just like with the RE:
if "string" in "here is first string".split(): ...
Jun 13 '06 #11

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

Similar topics

7
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...
17
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,...
51
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...
18
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...
32
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...
12
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(" &...
4
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...
6
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...
5
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...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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
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 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.