Skip to content

Commit 9d0d77b

Browse files
committed
Add naive regex in single-quoted strings
following regex operators
1 parent e62352f commit 9d0d77b

File tree

2 files changed

+195
-2
lines changed

2 files changed

+195
-2
lines changed

PowerShell.sublime-syntax

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,11 +850,21 @@ contexts:
850850
scope: keyword.operator.comparison.powershell
851851
captures:
852852
1: punctuation.definition.keyword.powershell
853-
- match: \B(-)(?i:[ic]?(?:not)?(?:like|match|contains|in)){{kebab_break}}
853+
- match: \B(-)(?i:[ic]?(?:not)?(?:match)){{kebab_break}}
854854
scope: keyword.operator.logical.powershell
855855
captures:
856856
1: punctuation.definition.keyword.powershell
857-
- match: \B(-)(?i:join|split|replace){{kebab_break}}
857+
push: expect-regex-string
858+
- match: \B(-)(?i:[ic]?(?:not)?(?:like|contains|in)){{kebab_break}}
859+
scope: keyword.operator.logical.powershell
860+
captures:
861+
1: punctuation.definition.keyword.powershell
862+
- match: \B(-)(?i:replace){{kebab_break}}
863+
scope: keyword.operator.string.powershell
864+
captures:
865+
1: punctuation.definition.keyword.powershell
866+
push: expect-regex-string
867+
- match: \B(-)(?i:join|split){{kebab_break}}
858868
scope: keyword.operator.string.powershell
859869
captures:
860870
1: punctuation.definition.keyword.powershell
@@ -995,6 +1005,18 @@ contexts:
9951005
- include: double-quoted-heredoc-strings
9961006
- include: single-quoted-heredoc-strings
9971007

1008+
expect-regex-string:
1009+
- match: '{{single_quote}}'
1010+
scope: meta.string.powershell string.quoted.single.powershell punctuation.definition.string.begin.powershell
1011+
embed: scope:source.regexp
1012+
embed_scope: meta.string.powershell source.regexp.embedded
1013+
escape: ({{single_quote}})
1014+
escape_captures:
1015+
1: meta.string.powershell string.quoted.single.powershell punctuation.definition.string.end.powershell
1016+
- include: comma-separators
1017+
- include: else-pop
1018+
- include: pop-eol
1019+
9981020
single-quoted-strings:
9991021
- match: '{{single_quote}}'
10001022
scope: punctuation.definition.string.begin.powershell

Tests/syntax_test_strings.ps1

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,177 @@
238238
"@
239239

240240

241+
###[ Regular Expressions ]#####################################################
242+
243+
'book' -match 'oo'
244+
# ^^^^^^ meta.string.powershell string.quoted.single.powershell
245+
# ^ punctuation.definition.string.begin.powershell
246+
# ^ punctuation.definition.string.end.powershell
247+
# ^^^^^^ keyword.operator.logical.powershell
248+
# ^ punctuation.definition.keyword.powershell
249+
# ^^^^ meta.string.powershell
250+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
251+
# ^^ source.regexp.embedded
252+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
253+
'big' -match 'b[iou]g'
254+
# ^^^^^ meta.string.powershell string.quoted.single.powershell
255+
# ^ punctuation.definition.string.begin.powershell
256+
# ^ punctuation.definition.string.end.powershell
257+
# ^^^^^^ keyword.operator.logical.powershell
258+
# ^ punctuation.definition.keyword.powershell
259+
# ^^^^^^^^^ meta.string.powershell
260+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
261+
# ^^^^^^^ source.regexp.embedded
262+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
263+
42 -match '[0-9][0-9]'
264+
# ^^ meta.number.integer.decimal.powershell constant.numeric.value.powershell
265+
# ^^^^^^ keyword.operator.logical.powershell
266+
# ^ punctuation.definition.keyword.powershell
267+
# ^^^^^^^^^^^^ meta.string.powershell
268+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
269+
# ^^^^^^^^^^ source.regexp.embedded
270+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
271+
'a1\ ' -match '....'
272+
# ^^^^^^ meta.string.powershell string.quoted.single.powershell
273+
# ^ punctuation.definition.string.begin.powershell
274+
# ^ punctuation.definition.string.end.powershell
275+
# ^^^^^^ keyword.operator.logical.powershell
276+
# ^ punctuation.definition.keyword.powershell
277+
# ^^^^^^ meta.string.powershell
278+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
279+
# ^^^^ source.regexp.embedded keyword.other.any.regexp
280+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
281+
'ACCOUNT NAME: Administrator' -match 'ACCOUNT NAME:\s*\w*'
282+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.powershell string.quoted.single.powershell
283+
# ^ punctuation.definition.string.begin.powershell
284+
# ^ punctuation.definition.string.end.powershell
285+
# ^^^^^^ keyword.operator.logical.powershell
286+
# ^ punctuation.definition.keyword.powershell
287+
# ^^^^^^^^^^^^^^^^^^^^^ meta.string.powershell
288+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
289+
# ^^^^^^^^^^^^^^^^^^^ source.regexp.embedded
290+
# ^^ keyword.control.character-class.regexp
291+
# ^ keyword.operator.quantifier.regexp
292+
# ^^ keyword.control.character-class.regexp
293+
# ^ keyword.operator.quantifier.regexp
294+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
295+
'SERVER01' -match '[A-Z]+-?\d\d'
296+
# ^^^^^^^^^^ meta.string.powershell string.quoted.single.powershell
297+
# ^ punctuation.definition.string.begin.powershell
298+
# ^ punctuation.definition.string.end.powershell
299+
# ^^^^^^ keyword.operator.logical.powershell
300+
# ^ punctuation.definition.keyword.powershell
301+
# ^^^^^^^^^^^^^^ meta.string.powershell
302+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
303+
# ^^^^^^^^^^^^ source.regexp.embedded
304+
# ^^^^^ meta.set.regexp
305+
# ^ keyword.operator.quantifier.regexp
306+
# ^ keyword.operator.quantifier.regexp
307+
# ^^^^ keyword.control.character-class.regexp
308+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
309+
'111-222-3333' -match '\d{3}-\d{3}-\d{4}'
310+
# ^^^^^^^^^^^^^^ meta.string.powershell string.quoted.single.powershell
311+
# ^ punctuation.definition.string.begin.powershell
312+
# ^ punctuation.definition.string.end.powershell
313+
# ^^^^^^ keyword.operator.logical.powershell
314+
# ^ punctuation.definition.keyword.powershell
315+
# ^^^^^^^^^^^^^^^^^^^ meta.string.powershell
316+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
317+
# ^^^^^^^^^^^^^^^^^ source.regexp.embedded
318+
# ^^ keyword.control.character-class.regexp
319+
# ^^^ keyword.operator.quantifier.regexp
320+
# ^^ keyword.control.character-class.regexp
321+
# ^^^ keyword.operator.quantifier.regexp
322+
# ^^ keyword.control.character-class.regexp
323+
# ^^^ keyword.operator.quantifier.regexp
324+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
325+
'fishing' -match '^fish$'
326+
# ^^^^^^^^^ meta.string.powershell string.quoted.single.powershell
327+
# ^ punctuation.definition.string.begin.powershell
328+
# ^ punctuation.definition.string.end.powershell
329+
# ^^^^^^ keyword.operator.logical.powershell
330+
# ^ punctuation.definition.keyword.powershell
331+
# ^^^^^^^^ meta.string.powershell
332+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
333+
# ^^^^^^ source.regexp.embedded
334+
# ^ keyword.control.anchor.regexp
335+
# ^ keyword.control.anchor.regexp
336+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
337+
'3.141' -match '3\.\d{2,}'
338+
# ^^^^^^^ meta.string.powershell string.quoted.single.powershell
339+
# ^ punctuation.definition.string.begin.powershell
340+
# ^ punctuation.definition.string.end.powershell
341+
# ^^^^^^ keyword.operator.logical.powershell
342+
# ^ punctuation.definition.keyword.powershell
343+
# ^^^^^^^^^^^ meta.string.powershell
344+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
345+
# ^^^^^^^^^ source.regexp.embedded
346+
# ^^ constant.character.escape.regexp
347+
# ^^ keyword.control.character-class.regexp
348+
# ^^^^ keyword.operator.quantifier.regexp
349+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
350+
'The last logged on user was CONTOSO\jsmith' -match '(.+was )(.+)'
351+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.powershell string.quoted.single.powershell
352+
# ^ punctuation.definition.string.begin.powershell
353+
# ^ punctuation.definition.string.end.powershell
354+
# ^^^^^^ keyword.operator.logical.powershell
355+
# ^ punctuation.definition.keyword.powershell
356+
# ^^^^^^^^^^^^^^ meta.string.powershell
357+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
358+
# ^^^^^^^^^^^^ source.regexp.embedded
359+
# ^ keyword.other.any.regexp
360+
# ^ keyword.operator.quantifier.regexp
361+
# ^ keyword.other.any.regexp
362+
# ^ keyword.operator.quantifier.regexp
363+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
364+
$string -match 'was (?<domain>.+)\\(?<user>.+)'
365+
# ^^^^^^^ variable.other.readwrite.powershell
366+
# ^ punctuation.definition.variable.powershell
367+
# ^^^^^^ keyword.operator.logical.powershell
368+
# ^ punctuation.definition.keyword.powershell
369+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.powershell
370+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
371+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.regexp.embedded
372+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
373+
'Hello World' -replace '(\w+) \w+', '$1 Universe'
374+
# ^^^^^^^^^^^^^ meta.string.powershell string.quoted.single.powershell
375+
# ^ punctuation.definition.string.begin.powershell
376+
# ^ punctuation.definition.string.end.powershell
377+
# ^^^^^^^^ keyword.operator.string.powershell
378+
# ^ punctuation.definition.keyword.powershell
379+
# ^^^^^^^^^^^ meta.string.powershell
380+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
381+
# ^^^^^^^^^ source.regexp.embedded
382+
# ^^ keyword.control.character-class.regexp
383+
# ^ keyword.operator.quantifier.regexp
384+
# ^^ keyword.control.character-class.regexp
385+
# ^ keyword.operator.quantifier.regexp
386+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
387+
# ^ punctuation.separator.sequence.powershell
388+
# ^^^^^^^^^^^^^ meta.string.powershell
389+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
390+
# ^^^^^^^^^^^ source.regexp.embedded
391+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
392+
"Hello World" -replace "(\w+) \w+", "`$1 Universe"
393+
'5.72' -replace '(.+)', '$$$1'
394+
# ^^^^^^ meta.string.powershell string.quoted.single.powershell
395+
# ^ punctuation.definition.string.begin.powershell
396+
# ^ punctuation.definition.string.end.powershell
397+
# ^^^^^^^^ keyword.operator.string.powershell
398+
# ^ punctuation.definition.keyword.powershell
399+
# ^^^^^^ meta.string.powershell
400+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
401+
# ^^^^ source.regexp.embedded
402+
# ^ keyword.other.any.regexp
403+
# ^ keyword.operator.quantifier.regexp
404+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
405+
# ^ punctuation.separator.sequence.powershell
406+
# ^^^^^^ meta.string.powershell
407+
# ^ string.quoted.single.powershell punctuation.definition.string.begin.powershell
408+
# ^^^^ source.regexp.embedded
409+
# ^ string.quoted.single.powershell punctuation.definition.string.end.powershell
410+
"5.72" -replace "(.+)", "`$`$`$1"
411+
241412
###[ String Formatting ]#######################################################
242413

243414
"{0:N2}" -f $a

0 commit comments

Comments
 (0)