如果文本包含某个字符,则在指定位置插入一个字符。

huangapple go评论59阅读模式
英文:

Insert a character in a position if the text contains

问题

要在特定位置插入双引号,如果列包含16.0、15.0等值,你可以如何操作?如果该位置已经存在双引号,是否有一种方法可以检查并忽略它?

我尝试了以下代码,但是如果尝试运行它,会出现错误:

#"Insert double quote" = Table.TransformColumns(#"Remove unwanted charachter", [Screen], 
    each if Text.Contains([Screen], "16.0") then Text.Insert(_, 5,"""")
    else if Text.Contains([Screen], "15.0") then Text.Insert(_, 5,"""")
    else if Text.Contains([Screen], "14.0") then Text.Insert(_, 5,"""")
    else [Screen]
    , type text)

请注意,这是一个示例代码,你需要根据你的实际情况进行修改。

英文:

So how do you insert a value like double quotes in a specific position if the column contains 16.0, 15.0 and so on? And is there a way where I can check and ignore if there is already a double quote in that position?

I have tried with this code but I get an error if I try it


#"Insert double quote" = Table.TransformColumns(#"Remove unwanted charachter", [Screen],
each if Text.Contains([Screen], "16.0") then Text.Insert(_, 5,"""")
else if Text.Contains([Screen], "15.0") then Text.Insert(_, 5,"""")
else if Text.Contains([Screen], "14.0") then Text.Insert(_, 5,"""")
else [Screen]
, type text)

答案1

得分: 2

我假设你可能有包含多个英寸表示数字的文本。如果是这样的话,也许正则表达式是一个不错的选择。在下面的示例中,我创建了一个名为'fnRegexReplace'的自定义函数,它引用了以下内容:

(x,y,z)=>
let 
   Source = Web.Page(
                     "<script>var x="&"'"&x&"'"&";var z="&"'"&z&
                     "'"&";var y=new RegExp('"&y&"','gi');
                     var b=x.replace(y,z);document.write(b);</script>")
                     [Data]{0}[Children]{0}[Children]{1}[Text]{0}
in 
   Source

然后,我创建了一个新的列来调用这个函数,以展示它的工作原理。

使用的表达式是\\b(\\d+(?:\\.\\d+)?)(?! x |\\))(?:"|inch)?(?:\\b|$),为了更好地标准化数据,我选择删除适用的情况下的单词'inch',并用双引号替换它。

英文:

> "I want to add a double quote after every 16.0 and 15.6...."

I'm assuming you may have text that would contain multiple inch representing figures. If that's the case, maybe a regular expression could be a good alternative here. In my example below I created a custom function called 'fnRegexReplace' which refers to:

(x,y,z)=>
let 
   Source = Web.Page(
                     "<script>var x="&"'"&x&"'"&";var z="&"'"&z&
                     "'"&";var y=new RegExp('"&y&"','gi');
                     var b=x.replace(y,z);document.write(b);</script>")
                     [Data]{0}[Children]{0}[Children]{1}[Text]{0}
in 
   Source

I then created a new column invoking this function to showcase how it works:

如果文本包含某个字符,则在指定位置插入一个字符。

如果文本包含某个字符,则在指定位置插入一个字符。

The expression used is \\b(\\d+(?:\\.\\d+)?)(?! x |\\))(?:"|inch)?(?:\\b|$) and would to standardize the data a little bit better I opted to remove the word 'inch' where applicable and replace it with the double quote.

答案2

得分: 1

尝试

#"Replace" = Table.TransformColumns("#NameofYourPriorStepGoesHere",{{"Screen", each if Text.Contains(_, "Inch", Comparer.OrdinalIgnoreCase) then _ else Text.Replace(Text.Replace(_, "15.0", "15.0""""), "16.0", "16.0""""), type text}})
英文:

Try

#"Replace" = Table.TransformColumns("#NameofYourPriorStepGoesHere",{{"Screen", each if Text.Contains(_,"Inch",Comparer.OrdinalIgnoreCase) then _ else Text.Replace(Text.Replace(_,"15.0","15.0"""),"16.0","16.0"""), type text}})

huangapple
  • 本文由 发表于 2023年7月27日 18:07:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定