【问题标题】:How can I extract a string and number from a larger based on a unique pattern in flutter?如何根据颤动中的独特模式从较大的字符串和数字中提取字符串和数字?
【发布时间】:2021-08-06 14:16:01
【问题描述】:

给定以下字符串结构:

Mark;12345 wrote:    // Username = Mark   ID# = 12345
Alex-Johnson;747645 wrote:       // Username = Alex-Johnson   ID# = 747645
Felix@felix.com;83213 wrote:    // Username = Felix@felix.com   ID# = 83213
Jack65;123123 wrote:      // Username = Jack65   ID# = 123123
John wrote:      // Username = John   ID# = null

理想情况下,我希望提取用户名和用户 ID,它们将由 ; 分隔。有时 ID# 也会像上一个示例字符串一样为空

有什么想法吗?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    // 你可以试试这个代码。这里我使用了 null-safety、regexp、list 和 maps

        void main()
        {
    
        String str = '''Mark;12345
        Alex-Johnson;747645
        Felix@felix.com;83213
        Jack65;123123
        John
        Bill
        ;1111''';
    
      Map<String?,String?> lineMaped= {};
      //This map will receive the username and the userid
      List<String> listOfEachLine= str.split('\n');
      //Each line of the text is put in the list
    
      RegExp searchForName= new RegExp(r'([\w._@-]+)(?:[;])([\d]*)');
      //this RegExp only have a match when the line has an ';' and a letter or digit 
      //before 
       for (int i=0;i < listOfEachLine.length;i++){
         RegExpMatch? match = searchForName.firstMatch(listOfEachLine[i]);
         //the firstMatch method can return null, for this situation I use the '?' 
           //in type declaration
         if(';'.allMatches(listOfEachLine[i]).length == 0){      
           //condition: there isn't any ';' in the line, in other words: when  ID# 
           //is blank
         lineMaped.addAll({listOfEachLine[i] : ''}); 
         } else {
             if (match != null && match.groupCount>1) { 
               //this if ensures the non-nullable of variables
             lineMaped.addAll({match.group(1): match.group(2)});
             
             } //if
           } //else
       }  //for
         lineMaped.forEach((k,v) => print('\n ${k} <-> ${v}'));
     } //main
    

    //输出:

    //马克 12345

    //亚历克斯-约翰逊 747645

    //Felix@felix.com 83213

    //Jack65 123123

    //约翰

    //账单

    【讨论】:

      【解决方案2】:

      尝试使用子字符串。我认为你也可以通过拆分来做到这一点。

      String text = 'Mark;12457 wrote:';
         if(text.contains(';')){
            int size = text.indexOf(';'); 
            int size2= text.indexOf('wrote:');
            String userName = text.substring(0,size);
            String id = text.substring(size+1,size2); //1 is the number of character in ('...')
            print('username: $userName, id#: $id');
        }else{
            int size= text.indexOf('wrote:');
            String userName = text.substring(0,size);
            print('username: $userName, id#: null');
          }
      

      【讨论】:

        【解决方案3】:

        更简单

        var str = 'Mark;12345 wrote:';
        final arr = str.replaceAll(' wrote:', '').split(';');
        final userName = arr[0];
        final userId = arr.length > 1 ? arr[1] : null;
        print('$userName   $userId');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-08-07
          • 1970-01-01
          • 2022-08-19
          • 2021-12-10
          • 1970-01-01
          • 2020-06-10
          • 2017-07-30
          • 2019-10-30
          相关资源
          最近更新 更多