Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Asterisks: Convert To Regexp

Asterisks: Convert To Regexp

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Xvordan   (29 posts)  Bio
Date Mon 11 Apr 2016 11:08 PM (UTC)
Message
Whenever I have a trigger/etc pattern which contains actual asterisks, the convert to regular expression function wants to represent them as (.*)? rather than as \*. Is there any reason that I'm missing for this behavior? (.*)? looks weird and inaccurate (at best) to me. I always have to go through the converted string and change the results to \*.
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 12 Apr 2016 01:19 AM (UTC)

Amended on Tue 12 Apr 2016 03:00 AM (UTC) by Nick Gammon

Message
The behaviour is correct. First, you need to make a "capture" which is why the brackets are there. If you don't happen to want a capture then you can omit the brackets.

For example:


Match: punch *     (non-regexp)
Match: punch (.*?) (regexp)
Send:  punch %1


The asterisk represents the capture into wildcard %1.

However if you just happen to have variable stuff that doesn't need capturing then .* is OK.




The "?" makes the wildcard non-greedy.

See my page about regular expressions: http://www.gammon.com.au/regexp


Groups and tagged expressions

The default behaviour is for each group to be returned as a "wildcard" for the trigger or alias...


Regexp: tell (.+) (.+)
Match : tell Nick hi there
Wildcard '1' = 'Nick hi'
Wildcard '2' = 'there'


However you can see a problem here, the first wildcard is "Nick hi" where we really want it to be the name. So we need to make it less greedy:


Regexp: tell (.+?) (.+)
Match : tell Nick hi there
Wildcard '1' = 'Nick'
Wildcard '2' = 'hi there'


The default for the non regexp wildcards is non-greedy matches, which is why the convert to regexp does that.

In fact, internally, all non-wildcard matches are converted using the same algorithm, so they are regexps by the time they are used.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,602 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.