Scripting Guide > Types of Data > Pattern Matching > Troubleshoot Patterns
发布日期: 09/18/2023

Troubleshoot Patterns

When a pattern isn’t working the way you expect, use Log() to send pieces of the match to the JMP log. The following example shows how to debug pattern matching and rewrite a script to account for the white space between words in the phrase “the quick brown fox”.

// greedy match for one or more characters except for white space

word = Pat Repeat( Pat Not Any( " " ) );
 
Pat Match(
	"the quick brown fox",
	word >> a +
	word >> b +
	word >> c +
	word >> d
);
 
Show( a, b, c, d );

a = "qu";

b = "i";

c = "c";

d = "k";

The output is probably not what you want, given the name of the pattern word. None of the words in the pattern were matched.

The Show( a, b, c, d ) expression is a good first step. If it doesn’t tell you how JMP got that answer, try inserting Log():

word = Pat Repeat( Pat Not Any( " " ) );
 
Pat Match(
	"the quick brown fox",
	(word >> a) >> Log( "a" ) 	+
	(word >> b) >> Log( "b" ) +
	(word >> c) >> Log( "c" ) +
	(word >> d) >> Log( "d" )
);
 
Show( a, b, c, d );

0(a) the (the number 0 is the position of the match)

0(a) th (the “a” is an aribtrary identifier you supplied to the log)

2(b) e

...

4(a) quick

4(a) quic

...

With this extra information, you can now see into the workings of the match, how the word pattern does, in fact, match a word, at least initially. But then it backs up and tries a smaller amount of the word so that the next word pattern can match some non-whitespace characters. However, there’s no provision in this pattern to match the white space between words. And that’s exactly what it did (find four consecutive words of one or more non-whitespace characters). Maybe the following script is what you need:

word = Pat Repeat( Pat Not Any( " " ) );
gap = Pat Repeat( Pat Any( " " ), 1, 999, GREEDY );
 
Pat Match(
	"the quick brown fox",
	word >> a + gap +
	word >> b + gap +
	word >> c + gap +
	word >> d
);
 
Show( a, b, c, d );

a = "the";

b = "quick";

c = "brown";

d = "fox";

需要更多信息?有问题?从 JMP 用户社区得到解答 (community.jmp.com).