I am trying to figure out how to can condense the number of pattern matching rules I need to create to pick up on conditions like I saw a man at the SW corner of Pike street. For “SW Corner”, I don’t want to write a match rule for every directional variation (N, S, E, W, etc.). I tried to do it with the below, but it isn’t right. Well, it works but it also picks up other things.
Example:
JavaScript
x
4
1
matcher.add("DIRECTION", None,
2
[{}, {"TEXT":{"REGEX":"(?:N)|(?:S)|(?:E)|(?:W)|(?:NW)|(?:NE)|(?:SW)|(?:SE)"}}, {"LOWER":"corner"}]
3
)
4
I want to be able to use the OR statements, but I am not sure how to do that with the single or double characters (N, S, E, W, NW, NE, SW, SE).
What am I doing wrong?
Advertisement
Answer
Do not overuse non-capturing groups, (?:SW)
is the same as SW
.
Also, you do not want to match SE
in SED
token, use anchors, ^
and $
.
Use
JavaScript
1
2
1
{"REGEX":"^(?:N|S|E|W|NW|NE|SW|SE)$"}
2
See proof.
Case insensitive variant:
JavaScript
1
2
1
{"REGEX":"(?i)^(?:N|S|E|W|NW|NE|SW|SE)$"}
2
Explanation
JavaScript
1
40
40
1
--------------------------------------------------------------------------------
2
^ the beginning of the string
3
--------------------------------------------------------------------------------
4
(?: group, but do not capture:
5
--------------------------------------------------------------------------------
6
N 'N'
7
--------------------------------------------------------------------------------
8
| OR
9
--------------------------------------------------------------------------------
10
S 'S'
11
--------------------------------------------------------------------------------
12
| OR
13
--------------------------------------------------------------------------------
14
E 'E'
15
--------------------------------------------------------------------------------
16
| OR
17
--------------------------------------------------------------------------------
18
W 'W'
19
--------------------------------------------------------------------------------
20
| OR
21
--------------------------------------------------------------------------------
22
NW 'NW'
23
--------------------------------------------------------------------------------
24
| OR
25
--------------------------------------------------------------------------------
26
NE 'NE'
27
--------------------------------------------------------------------------------
28
| OR
29
--------------------------------------------------------------------------------
30
SW 'SW'
31
--------------------------------------------------------------------------------
32
| OR
33
--------------------------------------------------------------------------------
34
SE 'SE'
35
--------------------------------------------------------------------------------
36
) end of grouping
37
--------------------------------------------------------------------------------
38
$ before an optional n, and the end of the
39
string
40