Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BollywooD

macrumors 6502
Original poster
Apr 27, 2005
372
46
Hamburg
I would like to exclude characters from a pattern match, using fnmatch.....


for example, I need to exclude matching on: all letters, all numbers, and the following – . %


where do I begin?

what about RegExKit?

would that be easier.
 
I would like to exclude characters from a pattern match, using fnmatch.....

for example, I need to exclude matching on: all letters, all numbers, and the following – . %

where do I begin?

Begin by reading the "Pattern Matching" section of the shell man page.

[...] Matches any one of the enclosed characters. A pair of charac-
ters separated by a hyphen denotes a range expression; any char-
acter that sorts between those two characters, inclusive, using
the current locale's collating sequence and character set, is
matched. If the first character following the [ is a ! or a ^
then any character not enclosed is matched.
...​
Emphasis added.

what about RegExKit?

would that be easier.
Do you already know how to write regex'es? If not, then it's unlikely that writing regex'es will be easier.
 
Begin by reading the "Pattern Matching" section of the shell man page.

[...] Matches any one of the enclosed characters. A pair of charac-
ters separated by a hyphen denotes a range expression; any char-
acter that sorts between those two characters, inclusive, using
the current locale's collating sequence and character set, is
matched. If the first character following the [ is a ! or a ^
then any character not enclosed is matched.
...​
Emphasis added.


Do you already know how to write regex'es? If not, then it's unlikely that writing regex'es will be easier.


Thanks, I didnt even think to check the man page ;)

would the following rule be valid:

Code:
[!a,z/!1,0/!–/!./!%]
to exclude matching on: all letters, all numbers, and the following – . %
:confused:
 
First question: What happens when you test it?

Second question: Exactly what are you trying to do with this exclusionary pattern matching?

Frankly, what you posted doesn't look anything like a shell matching pattern. That's why I asked what you're trying to do.
 
First question: What happens when you test it?

Second question: Exactly what are you trying to do with this exclusionary pattern matching?

Frankly, what you posted doesn't look anything like a shell matching pattern. That's why I asked what you're trying to do.


I want to match a string to an array of strings, however I dont want all letters, all numbers, and the following – . % included within that matching.


ie....

string:
would be a valid match

stringQ
string-
string9

would not

this seems to work, i think?
[!a-z][!0-9][!-.%]
 
I want to match a string to an array of strings, however I dont want all letters, all numbers, and the following – . % included within that matching.

ie....

string:
would be a valid match

stringQ
string-
string9

would not
?

This doesn't make sense. The fnmatch() function takes two textual args: one is a pattern, the other is a literal string to match against the pattern. In your description, you have a single string and an array of other strings. You don't have any patterns.

Unless your single string is a pattern, or your array contains patterns, I don't see how fnmatch() can possibly do anything for you.

Regex won't help either, because it's just fancier pattern matching. It adheres to the same structure as fnmatch: does string X match pattern Y.

If you want to use pattern-matching in any form, you have to somehow figure out which one of your inputs will be the pattern, and then figure out how to make it be a pattern.

So now I'm back to the "What are you trying to accomplish?" question.

Please explain what the single string represents, and what the array of strings represent (or what each string in the array represents), and what you're trying to accomplish by comparing or matching them. We may be able to suggest something. Without knowing what you're trying to accomplish, and based on what you've said so far, pattern-matching alone will never do what you seem to want.


this seems to work, i think?
Code:
[!a-z][!0-9][!-.%]
When I suggested testing it, I meant write a program and test the pattern you proposed against your actual input strings. If you have to say "this seems to work" or "I think?", then I can only assume you haven't performed an actual test of an actual program. Otherwise you'd have a definite answer: it works or it doesn't. And then if you'd posted "It doesn't work", I would have said "Post your code".
 
As far as I can tell, the code posted earlier does seem to work....
at least good enough, for what I want.
:eek:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.