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

Lie Lie.1296 at gmail.com
Sat Jul 26 12:48:18 CEST 2008


On Jul 26, 5:03 pm, gcmart... at 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)



More information about the Python-list mailing list