Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some regex patterns are not working #11

Open
ksmsk opened this issue Jan 14, 2020 · 3 comments
Open

Some regex patterns are not working #11

ksmsk opened this issue Jan 14, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@ksmsk
Copy link

ksmsk commented Jan 14, 2020

Hi,
There are some pattern issues i couldnt figure it out myself.

this is working as expected:

final str =  "--- spoiler ---\r\n\r\n spoiler content \r\n--- spoiler ---\r\n\r";
Iterable<Match> matches = RegExp(
            r"(---( )?(`)?spoiler(`)?( )?---)(.*?)(---( )?(`)?spoiler(`)?( )?---)",
            dotAll: true,
            multiLine: true,
            caseSensitive: false)
        .allMatches(str);
matches.forEach((m) => print(m.group(6)));
// output: "spoiler content"

however this is not working. renderText method is not called:

Widget build(BuildContext context) {
    return ParsedText(
      text: str,
      style: TextStyle(color: Colors.black),
      parse: [
        MatchText(
            type: ParsedType.CUSTOM,
            pattern:
                r"(---( )?(`)?spoiler(`)?( )?---)(.*?)(---( )?(`)?spoiler(`)?( )?---)",
            regexOptions: RegexOptions(
              dotAll: true,
              multiLine: true,
              caseSensitive: false,
            ),
            style: TextStyle(
              color: Colors.red,
              fontSize: 10,
            ),
            renderText: ({String str, String pattern}) {
              Map<String, String> map = Map<String, String>();
              Match match = RegExp(pattern).firstMatch(str);
              map['display'] = "(${match.group(6).trim()})";
              map['value'] = match.group(6).trim();
              return map;
            },
            onTap: (url) {
              print(url);
            }),
      ],
    );

same goes for positive lookbehind
this is working:

final str =
        "(lookup: testtest1) content content (lookup: testtest2) content content";
    Iterable<Match> matches =
        RegExp(r"(?<=\(lookup:)(.*?)(?=\))").allMatches(str);
    matches.forEach((m) => print(m.group(0)));
// output: testtest1
// output: testtest2

this is not:

Widget build(BuildContext context) {
    return ParsedText(
      text: str,
      style: TextStyle(color: Colors.black),
      parse: [
        MatchText(
            type: ParsedType.CUSTOM,
            pattern:
                r"(?<=\(lookup:)(.*?)(?=\))",
            style: TextStyle(
              color: Colors.red,
              fontSize: 10,
            ),
            renderText: ({String str, String pattern}) {
              Map<String, String> map = Map<String, String>();
              Match match = RegExp(pattern).firstMatch(str);
              map['display'] = "(${match.group(2).trim()})";
              map['value'] = match.group(2).trim();
              return map;
            },
            onTap: (url) {
              print(url);
            }),
      ],
    );

im using flutter_parsed_text: ^1.2.3
sdk environment for flutter sdk: ">=2.5.2 <3.0.0"

@fayeed
Copy link
Owner

fayeed commented Jan 15, 2020

@ksmsk I don't why it's not working but I will take a look.

@pcvdheuvel
Copy link

pcvdheuvel commented Apr 18, 2020

@fayeed I have a similar issue using ParsedType.CUSTOM. The pattern is only recognized when the string does contain other words besides the expression. As can be seen in the screenshot below there is no issue when using the ParsedType.URL, but there is using my custom IP address pattern.

image

The code below shows what I assign to the parse attribute of the ParsedText widget to get my example:

  List<MatchText> get parsePatterns => <MatchText>[
        // MatchText for url patterns
        MatchText(
            type: ParsedType.URL,
            style: _urlStyle,
            onTap: (String url) async {
              String _url = url.toLowerCase();
              // If the url is of type ParsedType.URL but cannot be launched add https://
              if (!(_url.contains('https://') || _url.contains('http://')) &&
                  !(_url.contains('mailto:') ||
                      _url.contains('tel:') ||
                      _url.contains('sms:'))) {
                _url = 'http://' + _url;
              }

              // Try to launch the url using `package:url_launcher`
              if (await canLaunch(_url)) {
                await launch(_url);
              } else {
                throw 'Could not launch $_url';
              }
            }),
        // MatchText for IP addresses
        MatchText(
            type: ParsedType.CUSTOM,
            regexOptions: RegexOptions(caseSensitive: false),
            pattern:
                r"^(?:http|https):\/\/[\w\-_]+(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)",
            style: _urlStyle,
            onTap: (url) => print(url))
      ];

@fayeed fayeed added the bug Something isn't working label Jul 17, 2020
@paulocoutinhox
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants