最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - Regex to match quoted and non-quoted text, and not any part matches - Stack Overflow

programmeradmin0浏览0评论

Have given up on figuring this out on my own.

Sample string to work the regex on:

"this","is","a","test"

The below regex works fine for double quoted (once) and not quoted text.

[`""`]?is[`""`]?

However it matches part strings like is in this, in the sample string.

Looking to do the following, to delete (for example) "is" OR is in the sample string

$String = '"this`","is","a","test'

$ConstructedRegex = "[`""`]?is[`""`]?,?"

$String.replace($ConstructedRegex,"").trimstart(",").trimend(",")

Expected outcome: $String = "this","a","test"

Have given up on figuring this out on my own.

Sample string to work the regex on:

"this","is","a","test"

The below regex works fine for double quoted (once) and not quoted text.

[`""`]?is[`""`]?

However it matches part strings like is in this, in the sample string.

Looking to do the following, to delete (for example) "is" OR is in the sample string

$String = '"this`","is","a","test'

$ConstructedRegex = "[`""`]?is[`""`]?,?"

$String.replace($ConstructedRegex,"").trimstart(",").trimend(",")

Expected outcome: $String = "this","a","test"

Share Improve this question edited Feb 5 at 19:03 user66001 asked Feb 5 at 17:38 user66001user66001 9351 gold badge15 silver badges37 bronze badges 10
  • 1 Use: [`""]?\bis\b[`""]? – anubhava Commented Feb 5 at 17:53
  • 1 @anubhava That did it. Thanks! Want to make it an answer so I can upvote and accept? – user66001 Commented Feb 5 at 18:00
  • 1 @SantiagoSquarzon I have a solution from anubhava, but to satisfy what might be curiosity - Matching the words in between the commas, regardless of whether they are quoted or not. – user66001 Commented Feb 5 at 18:01
  • 1 @SantiagoSquarzon Done – user66001 Commented Feb 5 at 18:55
  • 1 @SantiagoSquarzon Sorry, updated. It is a string. – user66001 Commented Feb 5 at 19:07
 |  Show 5 more comments

1 Answer 1

Reset to default 2

First consideration is that .Replace is a string method for literal replacements, it doesn't work with regex. What you want to use is -replace, the regex operator for replacements.

As for how you could approach the problem, since is could be quoted or not and it could be anywhere, including the beginning or end of a string, probably the easiest way to approach it is by splitting on , then filtering where the token doesn't match ^"?is"?$ (-notmatch, just like the other comparison operators, can act as a filter when there is a collection on the left-hand side) and lastly joining back with ,:

'is,"this","is","a","test",is' -split ',' -notmatch '^"?is"?$' -join ','

# Outputs: "this","a","test"

If you wanted to do it via replacement, you could perhaps use this pattern however I don't see a way to do it in one go (it requires a TrimEnd):

$result = 'is,"this","is","a","test",is' -replace '(?<=(?:^|,))"?is"?(?:,|$)'
$result.TrimEnd(',')
发布评论

评论列表(0)

  1. 暂无评论