.NET

Reply
Distinguished Contributor
jmaeding
Posts: 979
Registered: ‎12-27-2006
Message 1 of 10 (255 Views)

easiest way to do WCMATCH in .net?

255 Views, 9 Replies
03-25-2013 09:27 AM

I am aware of regex and several other string methods, but have not seen a built in way of checking against multiple patterns allowing for asterisks.

Curious what others have come up with.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 10 (245 Views)

Re: easiest way to do WCMATCH in .net?

03-25-2013 10:00 AM in reply to: jmaeding

Try asteriks preceded by "`" character, kinda "`*mybad"

if string.StartWith("`*mybad") etc

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Distinguished Contributor
jmaeding
Posts: 979
Registered: ‎12-27-2006
Message 3 of 10 (244 Views)

Re: easiest way to do WCMATCH in .net?

03-25-2013 10:03 AM in reply to: Hallex

I see, escape the *, good tip.

I will try that also with the contains method, thanks

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 10 (233 Views)

Re: easiest way to do WCMATCH in .net?

03-25-2013 11:11 AM in reply to: jmaeding

Yes, 'Contains' method will be handy in this case,

Cheers :smileyhappy:

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 5 of 10 (230 Views)

Re: easiest way to do WCMATCH in .net?

03-25-2013 11:20 AM in reply to: jmaeding

Exactly the same as (wcmatch ...) in AutoLisp and acutWcMatch/acutWcMatchEx in ObjectARX doing:

 

Autodesk.AutoCAD.Internal.Utils.WcMatch(string str, string pattern);
Autodesk.AutoCAD.Internal.Utils.WcMatchEx(string str, string pattern, bool ignoreCase);

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Distinguished Contributor
jmaeding
Posts: 979
Registered: ‎12-27-2006
Message 6 of 10 (218 Views)

Re: easiest way to do WCMATCH in .net?

03-25-2013 11:57 AM in reply to: Alexander.Rivilis

great suggestion, I saw your same post on theswamp from a while back.

I am actually doing a non-acad project, so have to use regular .net, but still cool to have access to acad's routines.

Valued Mentor
DiningPhilosopher
Posts: 370
Registered: ‎05-06-2012
Message 7 of 10 (198 Views)

Re: easiest way to do WCMATCH in .net?

03-25-2013 03:33 PM in reply to: Alexander.Rivilis

Yes, but that's not the 'easiest' way :smileyvery-happy:

 

namespace System
{
   public static class WcMatchExtensionMethods
   {
      public static bool Matches( this string str, string pattern, bool ignoreCase )
      {
         return Autodesk.AutoCAD.Internal.Utils.WcMatchEx( str, pattern, ignoreCase );
      }

      public static bool Matches( this string str, string pattern )
      {
         return Autodesk.AutoCAD.Internal.Utils.WcMatchEx( str, pattern, true );
      }
   }
}

 

Valued Mentor
jeff
Posts: 330
Registered: ‎05-12-2009
Message 8 of 10 (192 Views)

Re: easiest way to do WCMATCH in .net?

03-25-2013 03:50 PM in reply to: jmaeding

Honestly I have never really used Autolisp or Regex either.

This is only from 5 minutes a messing around since I thought it might be good to see what Regex was about. I only knew it had something to do with matching character in strings, but taking a quick look at WCMATCH and this article Converting Wildcards to Regexes this might be a start, but there are guys with AutoLisp and Regex experience around here and other forums that would much know a much better approach.

 

A little console program playing around

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            string poo = "I have no idea and I have spent 5 minutes on this and have no idea if it will work";
            Console.WriteLine(poo.WCMATCH("*z"));/////////////////////->False
            Console.WriteLine(poo.WCMATCH("*al"));////////////////////->False
            Console.WriteLine(poo.WCMATCH("min???s"));////////////////->True
            Console.WriteLine(poo.WCMATCH("*z", "*al", "min???s"));///->True
            Console.WriteLine(poo.WCMATCH("*z", "*al", "*ha"));///->True
                                                                   
            Console.ReadLine();
        }
    }

    public static class StringExtensions
    {
        public static bool WCMATCH(this string text, params string[] patterns)
        {
            foreach (string pat in patterns)
            {
                string reg = s_wildcardToRegex(pat);
                if (Regex.Match(text, reg).Success)
                {
                    return true;
                }
            }

            return false;
        }
///////////source http://www.codeproject.com/Articles/11556/Converting-Wildcards-to-Regexes
        private static string s_wildcardToRegex(string pattern)
        {
            return Regex.Escape(pattern).Replace("\\*", ".*").Replace("\\?", ".");
        }
    }
}

 

 

 *********Edit**********

DiningPhilosopher slipped in while I was posting and prime of example of what I meant when I I said other guys would know much better than I would. Actually I never said that but typed it.

You can also find your answers @ TheSwamp
Distinguished Contributor
jmaeding
Posts: 979
Registered: ‎12-27-2006
Message 9 of 10 (184 Views)

Re: easiest way to do WCMATCH in .net?

03-25-2013 04:00 PM in reply to: jeff

wow, thanks for all the responses.

Moderator
Alexander.Rivilis
Posts: 1,417
Registered: ‎04-09-2008
Message 10 of 10 (174 Views)

Re: easiest way to do WCMATCH in .net?

03-25-2013 05:12 PM in reply to: DiningPhilosopher

DiningPhilosopher wrote:

Yes, but that's not the 'easiest' way 


Oh, Tony! Of course your version is beautiful and laconic. For this you have new kudos from me! :smileyhappy:

 


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.