Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
-
bgbraga
- Forum Newbie
- Posts: 2
- Joined: Sun Nov 22, 2009 12:28 pm
Post
by bgbraga »
Hi,
Someone can help me?
I would like to create a regexp to get "class" definition of this text:
Code: Select all
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
My regexp should find this:
Code: Select all
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
So far my regexp looks like this:
Code: Select all
class (.*?)\(models\.Model\):[\n\t\r\s]+[\w\d\.]+[\s]*=[\s]*[\x20\d\w().=',]+
-
MichaelR
- Forum Contributor
- Posts: 148
- Joined: Sat Jan 03, 2009 3:27 pm
Post
by MichaelR »
Code: Select all
$text = "from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()";
$class = preg_split('/[\s]class /', $text);
echo $class[1];
// Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published')
... or are you just looking to find the class
name?
-
bgbraga
- Forum Newbie
- Posts: 2
- Joined: Sun Nov 22, 2009 12:28 pm
Post
by bgbraga »
No...
I would like to get the full class definition, like that:
Code: Select all
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
But I already done this.
Thanks.