如果找不到匹配项,正则表达式会返回python什么?

时间:2019-08-16 17:41:52

标签: python regex

如果在字符串中找不到匹配项,则正则表达式会返回给Python什么?

我正在尝试在python中编写if函数。我有一个包含2个不同字符串结尾的列表,我正在尝试测试第一个正则表达式,如果找不到任何东西,请使用else运行其他正则表达式搜索。

还有其他方法可以解决这个问题吗?

列表中的字符串结尾为:

class SearchList extends StatefulWidget {
  SearchList({ Key key }) : super(key: key);
  @override
  _SearchListState createState() => new _SearchListState();

}

class _SearchListState extends State<SearchList>
{
  Widget appBarTitle = new Text("Search Sample", style: new 
  TextStyle(color: Colors.white),);
  Icon actionIcon = new Icon(Icons.search, color: Colors.white,);
  final key = new GlobalKey<ScaffoldState>();
  final TextEditingController _searchQuery = new TextEditingController();
  List<String> _list;
  bool _isSearching;
  String _searchText = "";

  _SearchListState() {
    _searchQuery.addListener(() {
      if (_searchQuery.text.isEmpty) {
        setState(() {
          _isSearching = false;
          _searchText = "";
        });
      }
      else {
        setState(() {
          _isSearching = true;
          _searchText = _searchQuery.text;
        });
      }
    });
  }

  @override
  void initState() {
    super.initState();
    _isSearching = false;
    init();

  }

  void init() {
    _list = List();
    _list.add("VIP #1");
    // ignore: unnecessary_statements, unused_label
    onTap: () => Congrats();
    _list.add("VIP #2");
    _list.add("VIP #3");
    _list.add("VIP #4");
    _list.add("VIP #5");
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: key,
      appBar: buildBar(context),
      body: new ListView(
        padding: new EdgeInsets.symmetric(vertical: 8.0),
        children: _isSearching ? _buildSearchList() : _buildList(),
      ),
    );
  }

  List<ChildItem> _buildList() {
    return _list.map((contact) => new ChildItem(contact)).toList();
  }

  List<ChildItem> _buildSearchList() {
    if (_searchText.isEmpty) {
      return _list.map((contact) => new ChildItem(contact))
          .toList();
    }
    else {
      List<String> _searchList = List();
      for (int i = 0; i < _list.length; i++) {
        String  name = _list.elementAt(i);
        if (name.toLowerCase().contains(_searchText.toLowerCase())) {
          _searchList.add(name);
        }
      }
      return _searchList.map((contact) => new ChildItem(contact))
          .toList();
    }
  }

  Widget buildBar(BuildContext context) {
    return new AppBar(
        centerTitle: true,
        title: appBarTitle,
        actions: <Widget>[
          new IconButton(icon: actionIcon, onPressed: () {
            setState(() {
              if (this.actionIcon.icon == Icons.search) {
                this.actionIcon = new Icon(Icons.close, color: Colors.white,);
                this.appBarTitle = new TextField(
                  controller: _searchQuery,
                  style: new TextStyle(
                    color: Colors.white,

                  ),
                  decoration: new InputDecoration(
                      prefixIcon: new Icon(Icons.search, color: Colors.white),
                     hintText: "Search...",
                     hintStyle: new TextStyle(color: Colors.white)
                  ),
                );
                _handleSearchStart();
              }
              else {
                _handleSearchEnd();
              }
            });
          },),
        ]
    );
  }

  void _handleSearchStart() {
    setState(() {
      _isSearching = true;
    });
  }

  void _handleSearchEnd() {
    setState(() {
      this.actionIcon = new Icon(Icons.search, color: Colors.white,);
      this.appBarTitle =
      new Text("Search Sample", style: new TextStyle(color: Colors.white),);
      _isSearching = false;
      _searchQuery.clear();
    });
  }

}

class ChildItem extends StatelessWidget {
  final String name;
  ChildItem(this.name);
  @override
  Widget build(BuildContext context) {
    return new ListTile(title: new Text(this.name));
  }

}

我只是想退出公司A和公司B

我已经在if函数中尝试过1. ...at Company A; Price $84 2. ...at Company B

这是我的Regex模式代码。当我分别测试整个列表时,这两种模式都起作用:

== None, [], '', False

我试图弄清楚如果正则表达式找不到匹配项,将什么放在哪里,让分析公司获取以价格结尾的值,并让[]以2结尾,对于带有'company'的字符串

2 个答案:

答案 0 :(得分:3)

它返回具有匹配序列的列表。如果没有匹配项,则列表将为空。

要测试列表是否为空:

if len(analystcompanypattern_noPrice.findall(test)) == 0:

答案 1 :(得分:1)

您可以尝试(无需条件)

re.search('at ([^;]*)',str)



>>> str='...at Company B'

>>> m = re.search('at ([^;]*)',str)

>>> m.group(1)

>>> 'Company B'
>>> 
>>> 

>>> str='...at Company A;price 2'

>>> m = re.search('at ([^;]*)',str)

>>> m.group(1)

>>> 'Company A'