小编Hrv*_*jar的帖子

正则表达式匹配两个字符串?

我似乎找不到像以下示例中提取所有注释的方法.

>>> import re
>>> string = '''
... <!-- one 
... -->
... <!-- two -- -- -->
... <!-- three -->
... '''
>>> m = re.findall ( '<!--([^\(-->)]+)-->', string, re.MULTILINE)
>>> m
[' one \n', ' three ']
Run Code Online (Sandbox Code Playgroud)

two -- --由于正则表达式错误,阻止与最不匹配.有人可以指出我正确的方向如何提取两个字符串之间的匹配.


嗨,我已经测试了你们在评论中建议的内容......这里是工作解决方案,几乎没有升级.

>>> m = re.findall ( '<!--(.*?)-->', string, re.MULTILINE)
>>> m
[' two -- -- ', ' three ']
>>> m = re.findall ( '<!--(.*\n?)-->', string, re.MULTILINE)
>>> m
[' one \n', ' two -- …
Run Code Online (Sandbox Code Playgroud)

python regex regex-negation python-3.x

18
推荐指数
1
解决办法
3万
查看次数

从dictionary/JSON构造层次结构

我正在寻找一种方法来创建相同类的两个或多个实例之间的子父关系形式的层次结构.

如何在嵌套字典中创建这样的对象,例如?这甚至可能吗?是否还有其他方法可以推荐这样的任务?

# -*- coding: utf-8 -*-

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, exists
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.types import Integer, String

Base = declarative_base()

class Person(Base):
    __tablename__ = 'person';
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    parent_id = Column(Integer, ForeignKey('person.id'))

    def __init__(self, **kwargs):
        self.parent_id = kwargs.get('parent_id', None)
        self.name = kwargs.get('name')
        self.team = kwargs.get('team', [])
        # Is it possible to create more object of this type
        # and establish that …
Run Code Online (Sandbox Code Playgroud)

python recursion sqlalchemy parent-child class-hierarchy

7
推荐指数
1
解决办法
262
查看次数