Re: regex problem in django
On Jun 29, 2:57 pm, Joru wrote: > Still doesn't work even I remove wrap function :( So it seems your regex is incorrect, the problem is not related to Django. If you paste your code here I can have a look, but I still think you'd be better off reading a bit more about regular expressions yourself. The last link I posted wasn't very useful for learning because it is just a reference, there is a proper tutorial on regular expressions with Python here: http://www.amk.ca/python/howto/regex/ James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: regex problem in django
Still doesn't work even I remove wrap function :( On Jun 29, 7:35 pm, James Gregory wrote: > On Jun 29, 1:05 pm, Joru wrote: > > > I'm sorry for my typo > > the string var suppose to be like this > > str = "wr:\n one bunny \n two bunny \n wr:\n three bunny \n > > So every match string "wr:" should had "+" in front of it line > > the one that confuse me is that my function work in django/python > > shell, but thisregexdoesn't work well if i called it from views.py > > of my django app > > So the problem that I faced is that, how come when using django/python > > shell. myregexwork but not if I put in on my views.py > > Any hint? > > I may be wrong, but I suspect there is a difference between the input > string/code you are using in the shell, and the input string/code you > are using in the view. What happens if make a new empty Python script > and cut and paste in your code (removing the wrap function from > Django). Does it work then? > > James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: regex problem in django
On Jun 29, 1:05 pm, Joru wrote: > I'm sorry for my typo > the string var suppose to be like this > str = "wr:\n one bunny \n two bunny \n wr:\n three bunny \n > So every match string "wr:" should had "+" in front of it line > the one that confuse me is that my function work in django/python > shell, but this regex doesn't work well if i called it from views.py > of my django app > So the problem that I faced is that, how come when using django/python > shell. my regex work but not if I put in on my views.py > Any hint? I may be wrong, but I suspect there is a difference between the input string/code you are using in the shell, and the input string/code you are using in the view. What happens if make a new empty Python script and cut and paste in your code (removing the wrap function from Django). Does it work then? James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: regex problem in django
I'm sorry for my typo the string var suppose to be like this str = "wr:\n one bunny \n two bunny \n wr:\n three bunny \n So every match string "wr:" should had "+" in front of it line the one that confuse me is that my function work in django/python shell, but this regex doesn't work well if i called it from views.py of my django app So the problem that I faced is that, how come when using django/python shell. my regex work but not if I put in on my views.py Any hint? On Jun 29, 5:49 pm, James Gregory wrote: > On Jun 29, 11:19 am, Joru wrote: > > > ah, I just want to match in the end of line only > > so change the rule "wr$" would get what I want? > > > > Square brackets are for character groups, not literal strings. "wr:" > > > is just a string so it should be "wr:", not "[wr:]". Also, you want to > > > match the beginning of the line, not the end, so it should be > > > something like "^wr:", not "wr$". Unless I've missed something. > > When you split your test string on newlines you get: > > "wr:" > " one bunny" > " two bunny" > " wr: three bunny" > > In the first line "wr:" is at both the beginning and end of the > string, as it is the whole string. The next two lines do not feature > "wr:" at all. On the last line "wr:" is at neither the beginning nor > the end of the string - it has " " in front, and " three bunny" > afterwards. Depending on what you want to do you might want to call > strip() on each line, in which case the 4th line would become "wr: > three bunny", making wr: the beginning of the line. It depends what > you want to do, and anyway I can't write your program for you. > > Essentially, you just need to spend some time reading a bit more about > regular expressions:http://docs.python.org/library/re.html > > James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: regex problem in django
On Jun 29, 11:19 am, Joru wrote: > ah, I just want to match in the end of line only > so change the rule "wr$" would get what I want? > > > Square brackets are for character groups, not literal strings. "wr:" > > is just a string so it should be "wr:", not "[wr:]". Also, you want to > > match the beginning of the line, not the end, so it should be > > something like "^wr:", not "wr$". Unless I've missed something. When you split your test string on newlines you get: "wr:" " one bunny" " two bunny" " wr: three bunny" In the first line "wr:" is at both the beginning and end of the string, as it is the whole string. The next two lines do not feature "wr:" at all. On the last line "wr:" is at neither the beginning nor the end of the string - it has " " in front, and " three bunny" afterwards. Depending on what you want to do you might want to call strip() on each line, in which case the 4th line would become "wr: three bunny", making wr: the beginning of the line. It depends what you want to do, and anyway I can't write your program for you. Essentially, you just need to spend some time reading a bit more about regular expressions: http://docs.python.org/library/re.html James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: regex problem in django
ah, I just want to match in the end of line only so change the rule "wr$" would get what I want? On Jun 29, 4:56 pm, James Gregory wrote: > On Jun 29, 10:49 am, Joru wrote: > > > > > I mean None not null > > When I print rgx always None, that mean theregexeval never match > > The expected output result would be > > > +wr:> one bunny > > > two bunny > > > + wr: three bunny > > > Because everytime foundregexrules r'[wr:]$' then should add + in > > beginning of line > > > On Jun 29, 4:31 pm, James Gregory wrote: > > > > On Jun 29, 10:05 am, Joru wrote: > > > > > I still can't solve this > > > > Anyone had answer on this? > > > > > On Jun 26, 7:39 pm, Joru wrote: > > > > > > Hi, > > > > > > I experience some weirdness regarding usingregexwith django > > > > > I have following function in utils.py > > > > > > from django.utils.text import wrap > > > > > import re > > > > > > str = "wr: \n one bunny \n two bunny \n wr: three bunny \n > > > > > def do_regex(text): > > > > > lines = wrap(text, 55).split('\n') > > > > > for i, line in enumerate(lines): > > > > > cmp = re.compile(r'[wr:]$') > > > > > rgx = cmp.search(line) > > > > > if rgx: > > > > > line = "+%s" % line > > > > > lines[i] = line > > > > > else : > > > > > lines[i] = ">%s" % line > > > > > return '\n'.join(lines) > > > > > do_regex(str) > > > > > > when calling do_regex() from views.py, I always get rgx null while > > > > > when I use django shell rgx will have value when match toregexthat I > > > > > declare in cmp var > > > > > How to fix myregexso it can work inside views.py? > > > > I copied and pasted your code (with an added speech mark to close str) > > > into a controller function, and it works for me: > > > > Django version 1.1 beta 1, using settings 'pilchard.settings' > > > Development server is running athttp://127.0.0.1:8000/ > > > Quit the server with CTRL-BREAK.>wr: > > > > one bunny > > > > two bunny > > > > wr: three bunny > > > > [29/Jun/2009 10:29:45] "GET / HTTP/1.1" 200 2285 > > > > What do you mean by "get rgx null"? What exactly is null? Where? > > > > James > > Square brackets are for character groups, not literal strings. "wr:" > is just a string so it should be "wr:", not "[wr:]". Also, you want to > match the beginning of the line, not the end, so it should be > something like "^wr:", not "wr$". Unless I've missed something. > > James > James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: regex problem in django
On Jun 29, 10:49 am, Joru wrote: > I mean None not null > When I print rgx always None, that mean the regex eval never match > The expected output result would be > > +wr:> one bunny > > two bunny > > + wr: three bunny > > Because everytime found regex rules r'[wr:]$' then should add + in > beginning of line > > On Jun 29, 4:31 pm, James Gregory wrote: > > > On Jun 29, 10:05 am, Joru wrote: > > > > I still can't solve this > > > Anyone had answer on this? > > > > On Jun 26, 7:39 pm, Joru wrote: > > > > > Hi, > > > > > I experience some weirdness regarding usingregexwith django > > > > I have following function in utils.py > > > > > from django.utils.text import wrap > > > > import re > > > > > str = "wr: \n one bunny \n two bunny \n wr: three bunny \n > > > > def do_regex(text): > > > > lines = wrap(text, 55).split('\n') > > > > for i, line in enumerate(lines): > > > > cmp = re.compile(r'[wr:]$') > > > > rgx = cmp.search(line) > > > > if rgx: > > > > line = "+%s" % line > > > > lines[i] = line > > > > else : > > > > lines[i] = ">%s" % line > > > > return '\n'.join(lines) > > > > do_regex(str) > > > > > when calling do_regex() from views.py, I always get rgx null while > > > > when I use django shell rgx will have value when match toregexthat I > > > > declare in cmp var > > > > How to fix myregexso it can work inside views.py? > > > I copied and pasted your code (with an added speech mark to close str) > > into a controller function, and it works for me: > > > Django version 1.1 beta 1, using settings 'pilchard.settings' > > Development server is running athttp://127.0.0.1:8000/ > > Quit the server with CTRL-BREAK.>wr: > > > one bunny > > > two bunny > > > wr: three bunny > > > [29/Jun/2009 10:29:45] "GET / HTTP/1.1" 200 2285 > > > What do you mean by "get rgx null"? What exactly is null? Where? > > > James > Square brackets are for character groups, not literal strings. "wr:" is just a string so it should be "wr:", not "[wr:]". Also, you want to match the beginning of the line, not the end, so it should be something like "^wr:", not "wr$". Unless I've missed something. James James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: regex problem in django
I mean None not null When I print rgx always None, that mean the regex eval never match The expected output result would be +wr: > one bunny > two bunny + wr: three bunny Because everytime found regex rules r'[wr:]$' then should add + in beginning of line On Jun 29, 4:31 pm, James Gregory wrote: > On Jun 29, 10:05 am, Joru wrote: > > > > > I still can't solve this > > Anyone had answer on this? > > > On Jun 26, 7:39 pm, Joru wrote: > > > > Hi, > > > > I experience some weirdness regarding usingregexwith django > > > I have following function in utils.py > > > > from django.utils.text import wrap > > > import re > > > > str = "wr: \n one bunny \n two bunny \n wr: three bunny \n > > > def do_regex(text): > > > lines = wrap(text, 55).split('\n') > > > for i, line in enumerate(lines): > > > cmp = re.compile(r'[wr:]$') > > > rgx = cmp.search(line) > > > if rgx: > > > line = "+%s" % line > > > lines[i] = line > > > else : > > > lines[i] = ">%s" % line > > > return '\n'.join(lines) > > > do_regex(str) > > > > when calling do_regex() from views.py, I always get rgx null while > > > when I use django shell rgx will have value when match toregexthat I > > > declare in cmp var > > > How to fix myregexso it can work inside views.py? > > I copied and pasted your code (with an added speech mark to close str) > into a controller function, and it works for me: > > Django version 1.1 beta 1, using settings 'pilchard.settings' > Development server is running athttp://127.0.0.1:8000/ > Quit the server with CTRL-BREAK.>wr: > > one bunny > > two bunny > > wr: three bunny > > [29/Jun/2009 10:29:45] "GET / HTTP/1.1" 200 2285 > > What do you mean by "get rgx null"? What exactly is null? Where? > > James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: regex problem in django
On Jun 29, 10:05 am, Joru wrote: > I still can't solve this > Anyone had answer on this? > > On Jun 26, 7:39 pm, Joru wrote: > > > Hi, > > > I experience some weirdness regarding usingregexwith django > > I have following function in utils.py > > > from django.utils.text import wrap > > import re > > > str = "wr: \n one bunny \n two bunny \n wr: three bunny \n > > def do_regex(text): > > lines = wrap(text, 55).split('\n') > > for i, line in enumerate(lines): > > cmp = re.compile(r'[wr:]$') > > rgx = cmp.search(line) > > if rgx: > > line = "+%s" % line > > lines[i] = line > > else : > > lines[i] = ">%s" % line > > return '\n'.join(lines) > > do_regex(str) > > > when calling do_regex() from views.py, I always get rgx null while > > when I use django shell rgx will have value when match toregexthat I > > declare in cmp var > > How to fix myregexso it can work inside views.py? I copied and pasted your code (with an added speech mark to close str) into a controller function, and it works for me: Django version 1.1 beta 1, using settings 'pilchard.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. >wr: > one bunny > two bunny > wr: three bunny > [29/Jun/2009 10:29:45] "GET / HTTP/1.1" 200 2285 What do you mean by "get rgx null"? What exactly is null? Where? James --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: regex problem in django
I still can't solve this Anyone had answer on this? On Jun 26, 7:39 pm, Joru wrote: > Hi, > > I experience some weirdness regarding usingregexwith django > I have following function in utils.py > > from django.utils.text import wrap > import re > > str = "wr: \n one bunny \n two bunny \n wr: three bunny \n > def do_regex(text): > lines = wrap(text, 55).split('\n') > for i, line in enumerate(lines): > cmp = re.compile(r'[wr:]$') > rgx = cmp.search(line) > if rgx: > line = "+%s" % line > lines[i] = line > else : > lines[i] = ">%s" % line > return '\n'.join(lines) > do_regex(str) > > when calling do_regex() from views.py, I always get rgx null while > when I use django shell rgx will have value when match toregexthat I > declare in cmp var > How to fix myregexso it can work inside views.py? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
regex problem in django
Hi, I experience some weirdness regarding using regex with django I have following function in utils.py from django.utils.text import wrap import re str = "wr: \n one bunny \n two bunny \n wr: three bunny \n def do_regex(text): lines = wrap(text, 55).split('\n') for i, line in enumerate(lines): cmp = re.compile(r'[wr:]$') rgx = cmp.search(line) if rgx: line = "+%s" % line lines[i] = line else : lines[i] = ">%s" % line return '\n'.join(lines) do_regex(str) when calling do_regex() from views.py, I always get rgx null while when I use django shell rgx will have value when match to regex that I declare in cmp var How to fix my regex so it can work inside views.py? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---