简体   繁体   English

Powershell 正则表达式和拆分字符串

[英]Powershell Regex and Splitting a String

I have a column in a database table that I must convert to a comma delimited list (because I will be inserting each item in the list as a new record in a new table).我在数据库表中有一个列,我必须将其转换为逗号分隔的列表(因为我会将列表中的每个项目作为新表中的新记录插入)。 Show below are some examples of the data.下面是一些数据的例子。

Example data:示例数据:

  • 5209291 5847149,8943073, 8943895, 8963839 5209291 5847149、8943073、8943895、8963839
  • 3215874,2435245,2345563,634523,jim,jake,henry, school of health professions 3215874,2435245,2345563,634523,jim,jake,henry,健康职业学院

My requirements:我的要求:

  • The list must only be numbers列表只能是数字
  • the list must only be delimited by commas列表只能用逗号分隔

A perfect solution using the example above would be:使用上述示例的完美解决方案是:

  • 5209291,5847149,8943073,8943895,8963839 5209291,5847149,8943073,8943895,8963839
  • 3215874,2435245,2345563,634523 3215874、2435245、2345563、634523

What I've done so far is use String Split and hardcode the delimiters by passing them into the method.到目前为止,我所做的是使用 String Split 并通过将它们传递到方法中来硬编码分隔符。 This is not going to work because every day I get a feed from the DBA and the delimiter could potentially be anything.这是行不通的,因为每天我都会从 DBA 那里得到一个提要,而分隔符可能是任何东西。 I wish I could provide something that show "what I've done" but I'm just really at a loss here...我希望我能提供一些显示“我做了什么”的东西,但我真的很茫然……

You can match sequence of digits by this regular expression: \\d+ .您可以通过以下正则表达式匹配数字序列: \\d+ All you need is to match each sequence of digits in input string and join them by comma.您只需要匹配输入字符串中的每个数字序列并用逗号连接它们。

[regex]::Matches($InputString,'\d+')-join','

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM