473,387 Members | 1,517 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,387 software developers and data experts.

re.findall(a patern,'function(dsf sdf sdf)')

H!

First I have some random string below.

bla = """ <script type="text/javascript">
// <![CDATA[

var bla = new Blaobject("argh 1a", "argh 2a", "24", 24, 345)

function la( tec )
{
etc etc
}

function other thing( ){

var two = new BlaObject("argh 1b", "argh 2b", ""+(sv), ""+(2f),
"4");

bla die bla
}

// ]]>
</script> """
Now I'm trying to get each BlaObject with the first (variable)
function argument

And I can say that this isn't working
for a in re.findall(r'([BlaObject ])(.*)([)] *)',bla):
print a

The output must be something like:
# ('BlaObject','argh 1a')
# ('BlaObject','argh 1a')
or
# Blaobject("argh 1a", "argh 2a", "24", 24, 345)
# BlaObject("argh 1b", "argh 2b", ""+(sv), ""+(2f), "4");
My simple idea was to
a. the start position is the BlaObject
b. the stop position is the character ) (not ); because its a
javascript function)
c. the output [a (everything between) b]

Who knows the answer ?

Thanks very much,
GCMartijn
Jul 26 '08 #1
4 1219
Lie
On Jul 26, 5:03*pm, gcmart...@gmail.com wrote:
H!

First I have some random string below.

bla = """ * * * * *<script type="text/javascript">
* * * * * * * * // <![CDATA[

* * * * * * * * var bla = new Blaobject("argh 1a", "argh 2a", "24", 24, 345)

* * * * * * * * function la( tec )
* * * * * * * * {
* * * * * * * * * etc etc
* * * * * * * * }

* * * * * * * * function other thing( ){

* * * * * * * * * var two = new BlaObject("argh 1b", "argh 2b", ""+(sv), ""+(2f), "4");

* * * * * * * * * bla die bla
* * * * * * * * }

* * * * * * * * // ]]>
* * * * * </script* * * """

Now I'm trying to get each BlaObject with the first (variable)
function argument
First of all, since you're dealing with Javascript, which is case-
sensitive, Blaobject and BlaObject means different thing, if the dummy
code is real, it'd have raised a name not found error.
And I can say that this isn't working
for a in re.findall(r'([BlaObject ])(.*)([)] *)',bla):
* * print a
Of course that doesn't work, you've put BlaObject in a square bracket
(character class notation), which means the re module would search for
_a single letter_ that exist inside the square bracket. Then you do a
'.*', a greedy match-all, something that you generally don't want to
do. Next is the '[)] *', a character class containing only a single
character is the same as the character itself, and the zero-or-more-
repetition (*) is applied to the white space after the character
class, not to the character class itself.

In short, the regular expression you used doesn't seem to be an effort
to solve the problem. In other words, you haven't read the regular
expression docs: http://docs.python.org/lib/module-re.html . In other
words, it's useless to talk with you until then.

(snip)
Jul 26 '08 #2
In short, the regular expression you used doesn't seem to be an effort
to solve the problem. In other words, you haven't read the regular
expression docs:http://docs.python.org/lib/module-re.html. In other
words, it's useless to talk with you until then.
Its a combination
- I don't understand english very good (yet)
- For me its hard to learn the re , I will try to search again at
google for examples and do some copy past things.

Jul 26 '08 #3
gc*******@gmail.com wrote:
- For me its hard to learn the re , I will try to search again at
google for examples and do some copy past things.
this might be useful when figuring out how RE:s work:

http://kodos.sourceforge.net/

also, don't forget the following guideline:

"Some people, when confronted with a problem, think 'I know,
I'll use regular expressions.' Now they have two problems."

some advice:

- Keep the RE:s simple. You can often simplify things a lot by doing
multiple searches, or even by applying a second RE on the results from
the first. In this case, you could use one RE to search for BlaObject,
and then use another one to extract the first argument.

- Ordinary string methods (e.g. find, partition, split) are often a very
capable alternative (in combination with simple RE:s). In your case,
for JavaScript code that's as regular as the one in your example, you
can split the string on "BlaObject(" and then use partition to strip off
the first argument.

- only use RE:s to read specialized file formats if you know exactly
what you're doing; there's often a ready-made library that does it much
better.

- The regular expression machinery is not a parser. You cannot handle
all possible syntaxes with it, so don't even try.

</F>

Jul 26 '08 #4
On 26 jul, 14:25, Fredrik Lundh <fred...@pythonware.comwrote:
gcmart...@gmail.com wrote:
- For me its hard to learn the re , I will try to search again at
google for examples and do some copy past things.

this might be useful when figuring out how RE:s work:

* * *http://kodos.sourceforge.net/

also, don't forget the following guideline:

* * "Some people, when confronted with a problem, think 'I know,
* * I'll use regular expressions.' * Now they have two problems."

some advice:

- Keep the RE:s simple. *You can often simplify things a lot by doing
multiple searches, or even by applying a second RE on the results from
the first. *In this case, you could use one RE to search for BlaObject,
and then use another one to extract the first argument.

- Ordinary string methods (e.g. find, partition, split) are often a very
capable alternative (in combination with simple RE:s). *In your case,
for JavaScript code that's as regular as the one in your example, you
can split the string on "BlaObject(" and then use partition to strip off
the first argument.

- only use RE:s to read specialized file formats if you know exactly
what you're doing; there's often a ready-made library that does it much
better.

- The regular expression machinery is not a parser. *You cannot handle
all possible syntaxes with it, so don't even try.

</F>
Thanks for the info, I will download that program later so I can build
a re (i hope)

Because I can't wait for that re, I have made a non re solution what
is working for now.

for a in bla.split():
if a.find('BlaObject')<>-1:
print a[11:].replace("'","").replace('"',"").replace(",","")

(I know this is not the best way, but it helps me for now)
Jul 26 '08 #5

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

Similar topics

1
by: Roi | last post by:
Hello, I'm trying to implements Factory Patern using 2 DLL's. On the first DLL i implemented the Factory, and on the other I defines global variables which activating the Factory. the...
5
by: huiling25 | last post by:
I have a sdf file, i wan to retrieve product numbers from the file. However, when i print out, it only printed out the first product number(02230416222D). There are supposed to be 2 products numbers,...
0
by: ÂÀÏþ´¨ | last post by:
How can read .sdf(SqlServerCE) file from PC using csharp language.
1
by: sudhi8212 | last post by:
i want the code for searching patern for three fields as for textbox. as when open the naukri.com first that provide a fecility to search the job acording as joblocation, keyword , field etc.
0
by: Matt Porter | last post by:
On Sun, 18 May 2008 19:13:57 +0100, J. Clifford Dyer <jcd@sdf.lonestar.orgwrote: Thanks. Had to change a few bits to make it behave as I expected: def compress_str(s): # str is a builtin...
3
by: The Mad Ape | last post by:
Hi I have tables that I need to convert to dbf format from MS SQL Compact 2005 (sdf) format that reside on my desktop. I was hoping that I could embed the utility and call it from some code I...
4
by: jwebster | last post by:
I have a PDA application created with VB 2008. I used SDF files for my database. Within the database i have a table that has the following fields : Lot, Part, Expiration Date, Status. The user...
2
by: bobt1991 | last post by:
I just installed Microsoft Visual Studio 2008 Express edition, and it installs some sort of stripped down version of SQL Server. It comes with no tools for connecting to your "local" database,...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.