Anyone like regex challenges?

General discussions about Automagic and automation in general

Moderator: Martin

Post Reply
canadaDry
Posts: 24
Joined: 09 Feb 2018 18:38

Anyone like regex challenges?

Post by canadaDry » 04 Apr 2018 18:51

Ok, I'll admit I'm pretty lame at regex, so I'm probably missing something obvious (like look ahead/backward).

Trying to detect a single period ('.') at the end of the line. The difficulty I'm having is ensuring that the end of line doesn't end with '...'

I ended up resorting to this code:

Code: Select all

match_list = newList(null, null, null, null, null);
if (matches("The quick brown fox...", '(.+?)(\\.)(\\.*)(\\s?$)',match_list) == true and match_list[3] == "")
{
  // should be true for "jumped over the lazy dog."
}
else
{
 // should be false for "The quick brown fox..."
}
Play with this solution here:
https://regex101.com/r/ALGVUA/1

User avatar
Desmanto
Posts: 1931
Joined: 21 Jul 2017 17:50

Re: Anyone like regex challenges?

Post by Desmanto » 05 Apr 2018 06:52

Challenge Accepted :)

I am confused with the matched_list. You actually don't need to declare it again. Or simply declared it as matched_list = newList(); no inner element needed, as it will be replaced anyway.

And you don't even need the matched group, you only need to match the regex, no grouping needed.

Code: Select all

if(matches("The quick brown fox.", '[^.]+?\\.(?!\\.\\.)'))
{
  a = true;// should be true for "jumped over the lazy dog."
}
else
{
  a = false;// should be false for "The quick brown fox..."
}
When you need to match anything that is not dot, use [^.]+? this will match anything which is not dot.
Then the next \\. will match the last dot needed.
The (?!\\.\\.) is negative lookaround, will make sure if the last 3 character match with 3 dots (1 + 2), it will throw away the match.
2 dots at the end will also result in false, since it though it match partially, but it doesn't match all. You can check this in Automagic built-in regex tester. (just remember to change back the double backslash to single backslash)

This assume you don't have any dot in the middle of the sentence. However if you have it, you need to add another .+ to capture that and only stop at the last non dot capture.
the regex will be '.+[^.]+?\\.(?!\\.\\.)'
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190714, Pie 9.0, Rooted.

canadaDry
Posts: 24
Joined: 09 Feb 2018 18:38

Re: Anyone like regex challenges?

Post by canadaDry » 05 Apr 2018 07:36

Desmanto wrote:matched_list...don't need to declare it again... declare it as matched_list = newList();
Got it, thx!
Desmanto wrote:The (?!\\.\\.) is negative lookaround, will make sure if the last 3 character match with 3 dots (1 + 2), it will throw away the match.
Thanks! I figured that lookaround was going to be the trick... but I'm still pretty newbish when it comes to regex!
Desmanto wrote:This assume you don't have any dot in the middle of the sentence. However if you have it, you need to add another .+ to capture that and only stop at the last non dot capture.
Yes, it needs to ignore any other dots and only check the end of the string - hence the $ in my original expression.

By simply adding $ to the end of your expression it seems to do the trick! Woot!

Can see it here: https://regex101.com/r/ALGVUA/2

As usual you have great advice. Thanks Desmanto!

User avatar
Desmanto
Posts: 1931
Joined: 21 Jul 2017 17:50

Re: Anyone like regex challenges?

Post by Desmanto » 05 Apr 2018 13:57

You are welcome. Regex need so many usage cases to really grasp the idea. I will take any chance to learn it if I can.

Yes, the last $ will make sure it only match the last dot of the line.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190714, Pie 9.0, Rooted.

Post Reply