Netclasses - Wildcards

According to here, [Section: Netclasses], emphasis mine:

Net patterns can use both regular expressions and wildcards (* to match any number of any characters, including none, and ? to match zero or one of any character). The nets that match the selected pattern are displayed to the right of the pattern list.

I believe that the ? does not function properly for the 0 portion of 0 or 1 characters. Please see the following example.

Example:

• 1x Wildcard *: [Correct]
image

• 1x Wildcard ?: [Correct]
image

• 2x Wildcard ?: [Incorrect - Expecting +9V]

• 3x Wildcard ?: [Incorrect - Expecting +9V]
image

• 4x Wildcard ?: [Incorrect - Expecting +9V, +9V_0, +9V_1]
image

Question:

Is this a bug, or do I understand wildcards incorrectly?

I did a quick test in Linux command line. (‘Music’ only because it happened to be empty.)

eeli@eeli-ThinkStation-E31:~/Music$ touch +9V +9V_0 +9V_1
eeli@eeli-ThinkStation-E31:~/Music$ ls
+9V  +9V_0  +9V_1
eeli@eeli-ThinkStation-E31:~/Music$ ls +9V
+9V
eeli@eeli-ThinkStation-E31:~/Music$ ls +9?
+9V
eeli@eeli-ThinkStation-E31:~/Music$ ls +9??
ls: cannot access '+9??': No such file or directory
eeli@eeli-ThinkStation-E31:~/Music$ ls +9???
+9V_0  +9V_1
eeli@eeli-ThinkStation-E31:~/Music$ ls +9????
ls: cannot access '+9????': No such file or directory

It seems to handle consecutive ?s as exactly as many characters as there are ?s. This makes sense to me. But wildcard syntax is very simple and meant for simple cases. It looks like that if you want better “resolution” you have to add lines.

EDIT: actually the documentation may be wrong: ? is probably exactly one character. The syntax recognizes also []:

I have not looked into the details, but I think it follows regular expression syntax. I guess you think the question mark means “any character”, but for regular expressions it means:

The question mark indicates zero or one occurrences of the preceding element. For example, colou?r matches both “color” and “colour”.

Source:

In a regular expression, using multiple question marks in a row probably does not make sense at all.

But be careful with regular expressions. It works Ok-ish for relatively simple things, but those things can get very complicated fast, and there are different dialects for them, and that is also quite *&^%$#@!

1 Like

The documentation does not mention the syntax used; however, on the same page, above in [Section: Working With Symbols], the following is listed:

  • Regular expressions: if you’re familiar with regular expressions, these can be used too. The regular expression flavor used is the wxWidgets Advanced Regular Expression style, which is similar to Perl regular expressions.

If this is the syntax for netclasses, it wouldn’t hurt to repeat the syntax used in [Section: Netclasses].

No, this is more confusing. At the moment I think that both wildcard syntax and regex syntax are tried, and if either of them matches, it’s a match.

I have nets a, aa, aaa, abb.

Regex doesn’t explain ‘a??’ matching ‘abb’. Wildcard doesn’t explain ‘b?a’ matching ‘a’.

2 Likes

Was it @craftyjon who wrote the new matching system? I think this needs better explanation and documentation. The current documentation in ‘master’ says

Net patterns can use both regular expressions and wildcards (* to match any number of any characters, including none, and ? to match zero or one of any character).

which is now misleading as far as ? goes.

2 Likes

And then something for a smile.

(Hint: use the context menus.)

1 Like

In the Bourne shell implementation of wildcards which is the usual reference, * matches 0 or more of any character, but ? matches 1 of any character. It’s possible that the documentaiton is wrong on ? as you surmise.

Also I wonder how the implementation uses both wildcards and REs. Are they both applied and all results are collected? a*bb? could match abbc as wildcard or b as RE.

1 Like

I believe @JeffYoung wrote the matching system, and I wrote the doc in question. It’s very possible the doc is wrong :slight_smile:

My memory is hazy, it has been a while since I played with the system in detail, but I think it uses both regex and glob syntax? Edit: duh, it says that in the doc I wrote and eelik quoted. The question is how that works exactly.

1 Like

The matching system is actually quite old (implemented originally by Alexis, at least according to the file header).

But to clarify, yes netclass matching runs the wildcard matcher, the regex matcher, and the substring matcher, and considers it a match if any of them match.

In the wildcard matcher, ? means exactly one character.

In the regular expression matcher, ? means 0 or 1 of the preceeding clause.

4 Likes

And what is that?

Maybe a basic syntax help dialog would be good, like in the Custom Rules configuration page. A first time user may be lost with the netclass setup. Even sw engineers don’t necessarily remember everything about these matching engines and mixing them together can give weird results if you don’t know what to expect.

Reminder that you don’t have to use the netclass setup page: in many cases it is more clear to just use netclass assignment labels on the schematic.

That is a great function for finding things (such as symbol name searches in a library), but it is a horrible system for assigning net classes, and it should never have been re-used for this. I was also unpleasantly surprised by the change in how assigning net classes work. When I open an old project the old assignments seem to have broken too. I’m not sure if I’ll ever be able to appreciate the new method. I also do not know if KiCad developers are still willing to use a more unambiguous matching method now this has entered the stable version, and it has the potential to break existing projects if it’s changed now.

This is what netclass labels on the schematic are for: unambiguity.

This seems independent from any feedback on how pattern matching works. Please file a bug report with the old project that didn’t import correctly.

I think I agree, I’d have expected this to be very well defined. I had assumed it was just basic ?* wildcards.

And yeah, if it’s doing the triple match thing I did indeed originate that. There’s even a fourth matcher in that set that everyone forgets about (numeric range matching, where search term e.g. “Vcc>3” matches “Vcc=3.1” and so on), now I’m curious if this is running that matcher too :stuck_out_tongue:

Oops, I misread the code. We do not include the substring matcher for netclasses (that’s for signals in the simulator):

    switch( aContext )
    {
    case CTX_LIBITEM:
        // Whatever syntax users prefer, it shall be matched.
        AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
        AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
        AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_RELATIONAL>() );
        // If any of the above matchers couldn't be created because the pattern
        // syntax does not match, the substring will try its best.
        AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
        break;

    case CTX_NETCLASS:
        AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX_EXPLICIT>() );
        AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD_EXPLICIT>() );
        break;

    case CTX_SIGNAL:
        AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
        AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
        AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
        break;
    }

I was just going to say, it does not do substring matches in my tests, and that’s a good thing, because it would make it effectively impossible to exactly specify nets with the pattern rules. If you make an abc pattern, you want that to match only net abc, and not abcd etc.

Heh, I didn’t know about the range matching feature until I updated that section of the docs sometime last year. I had a “that’s pretty neat!” moment. It’s been well documented the whole time, I guess I had just never read that section.

1 Like

I hope it can be useful to someone. I wrote it years ago and never ended up even using it myself :woozy_face: