Home > database >  Why is My Regex Code not working on DART?
Why is My Regex Code not working on DART?

Time:01-08

I fetched data from a web sites body.Then I write a regular expression and applied on DART but it didnt work.What is the Problem?

Here is the Regex code:

</td><td align="left">(.*?)</td><td mover(this);" onm ouseout="mout(this);" style="background-color:White;">
            <td align="left">233</td><td align="left">ÖMER EFE CIKIT</td><td  align="center">

And the dart code:

void CheckRE(String text) {
    final RegExp pattern = RegExp(
      r'</td><td align="left">(.*?)</td><td ',
      multiLine: true,
      caseSensitive: true,
    ); // 800 is the size of each chun
    pattern
        .allMatches(text)
        .forEach((RegExpMatch match) => print(match.group(1)));
  }

CodePudding user response:

I think what you want is the following.

I have changed your output so it prints the content of capture group 1 instead of capture group 0. Capture group 0 contains the whole string which matches while 1 and up contains the content of each defined capture group in your regular expression.

const input = '''
</tr><tr onm ouseover="mover(this);" onm ouseout="mout(this);" style="background-color:White;">
            <td align="left">233</td><td align="left">ÖMER EFE CIKIT</td><td  align="center">
''';

void main() => checkRE(input); // ÖMER EFE CIKIT

void checkRE(String text) {
  final RegExp pattern = RegExp(
    r'</td><td align="left">(.*?)</td><td ',
    multiLine: true,
    caseSensitive: true,
  ); // 800 is the size of each chun
  pattern.allMatches(text).forEach((RegExpMatch match) => print(match[1]));
}

Also changed (.*) to (.*?) based on advice from @MikeM.

CodePudding user response:

Here is my full Code. I use Flutter

import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:webview_flutter/webview_flutter.dart';

class TestScreen extends StatefulWidget {
  TestScreen({Key? key}) : super(key: key);

  @override
  State<TestScreen> createState() => _TestScreenState();
}

class _TestScreenState extends State<TestScreen> {
  WebViewController? _controller;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Test"),
      ),
      body: WebView(
        initialUrl: 'https://e-okul.meb.gov.tr/logineOkul.aspx',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController webViewController) {
          // Get reference to WebView controller to access it globally
          _controller = webViewController;
        },
        javascriptChannels: <JavascriptChannel>[
          // Set Javascript Channel to WebView
          _extractDataJSChannel(context),
        ].toSet(),
        onPageStarted: (String url) {},
        onPageFinished: (String url) async {
          print('Page finished loading: $url');
          // In the final result page we check the url to make sure  it is the last page.
          if (url.contains('/OOK07003.aspx')) {
            await _controller!
                .runJavascriptReturningResult('document.body.innerHTML')
                .then(
                  (value) => CheckRE(value),
                );
          }
        },
      ),
    );
  }

  void CheckRE(String text) {
    final RegExp pattern = RegExp(
      r'</td><td align="left">(.*?)</td><td class="dataGridActive',
      multiLine: true,
      caseSensitive: true,
      unicode: true,
    );
    pattern
        .allMatches(text)
        .forEach((RegExpMatch match) => print(match.group(1)));
  }

  JavascriptChannel _extractDataJSChannel(BuildContext context) {
    return JavascriptChannel(
      name: 'Flutter',
      onMessageReceived: (JavascriptMessage message) {
        String pageBody = message.message;
        print('page body: $pageBody');
      },
    );
  }
}
  •  Tags:  
  • Related