使用正则表达式匹配两个逗号之间的字符串。

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

match string between two commas using regular expressions

问题

Sure, here is the translated portion of your request:

"大家好,
我有以下字符串,我想匹配包含" Body "后跟任何字符串直到逗号的字符串,如下所示"

"30英寸,球阀,悬臂式,通用,CL 900,BW,主体LTCS,金属座椅,齿轮操作,合金825修剪,全通,NACE MR 0175/ISO 15156 - 配有扩展的管件。VBFW 91Z08"

".75英寸,球阀,通用,CL 2500,法兰,RF,主体DSS,PEEK座椅,修剪DSS,减小通道 -770343.254.1"

"3.75英寸,球阀,通用,CL 2500,法兰,RF,摆动主体DSS,PEEK座椅,修剪DSS,减小通道 -770343.254.1"

"16英寸,球阀,悬臂式,通用,CL 900,法兰,RTJ,分体,主体CS +合金625Clad,软座椅,齿轮操作,修剪Inconel 625,全通,NACE MR 0175/ISO 15156 - 根据DATASHEET RE-185942"

我正在努力编写正则表达式,因为我刚开始学习它。任何帮助都将不胜感激。

以下是我迄今为止尝试的模式

(?<=,) Body +\w+

https://regex101.com/r/tMlQby/4

英文:

All,
I have the below strings and I want to match string that contains ", Body " followed by any string up to comma as highlighted in bold below

> 30 IN, Ball Valve, Trunnion, Gen manu, CL 900, BW, Body LTCS, Metal Seat, Gear Operated, Trim Alloy 825, Full Bore, NACE MR 0175/ISO 15156 -with extended pup-piece as pipe schedule. VBFW 91Z08

> .75 IN, Ball Valve, Gen manu, CL 2500, Flanged, RF, Body DSS, PEEK seats, Trim DSS, Reduced Bore -770343.254.1

> 3.75 IN, Ball Valve, Gen manu, CL 2500, Flanged, RF,Swing Body DSS, PEEK seats, Trim DSS, Reduced Bore -770343.254.1

> 16 IN, Ball Valve, Trunnion, Gen manu, CL 900, Flanged, RTJ, Split Body, Body CS + Alloy 625 Clad, Soft Seat, Gear Operated, Trim Inconel 625, Full Bore, NACE MR 0175/ISO 15156 -AS PER DATASHEET RE-185942

I am struggling to write a regex as I just started learning it. any help is greatly appreciated.

below is the pattern what I have tried so far

(?<=,) Body +\w+

https://regex101.com/r/tMlQby/4

答案1

得分: 2

不清楚你想要对 Swing Body 行执行什么操作,因为它不符合你所述的模式 , Body,但对于其他行,你可以使用以下正则表达式:

(?<=, )Body .+?(?=,)

. 将匹配任何字符,包括空格。? 表示将其设置为懒惰量词(在遇到第一个逗号时停止匹配)。而 ?= 是正向预查。

示例

英文:

It's not clear what you want to do with the Swing Body line, since that doesn't match your stated pattern of , Body, but for the others you can use:

(?&lt;=, )Body .+?(?=,)

. will match any character, including whitespace. ? means make it a lazy quantifier (stop at the first comma you run into). And ?= is a positive lookahead.

Demo

答案2

得分: 1

(?&lt;=,) 查找正向后面有逗号的部分
[\s\w]* 匹配任意数量的空白字符或字母数字字符
Body 匹配 Body 后跟一个空格字符
.*? 懒惰地匹配任何字符
(?=,) 查找正向前面有逗号的部分

英文:

You can use the regex, (?&lt;=,)[\s\w]*Body .*?(?=,)

Check this for a demo.

Explanation:

  1. (?&lt;=,) is looking behind positively for a comma
  2. [\s\w]* matches any number of whitespace character or a word character
  3. Body matches Body followed by a space character
  4. .*? matches any character lazily
  5. (?=,) is looking ahead positively for a comma

答案3

得分: 0

  1. (?&lt;=,) - 正向回顾断言,字符串必须在逗号之前。
  2. [^,]* - 匹配零个或多个非逗号字符。
  3. Body - 匹配 'Body'。
  4. [^,]+ - 匹配一个或多个非逗号字符。
  5. (?=,) - 正向预查断言,字符串必须在逗号之后。

如果您假设初始逗号后始终有一个空格,并且您 想匹配它,则使用:

(?&lt;=, )[^,]*Body[^,]+(?=,)

如果您假设初始逗号后 必须 有一个空格,并且您 希望 匹配它,则使用:

(?&lt;=,) [^,]*Body[^,]+(?=,)

英文:
(?&lt;=,)[^,]*Body[^,]+(?=,)

See Regex Demo

  1. (?&lt;=,) - Positive lookbehind assertion that the string must be preceded by a ','.
  2. [^,]* - Matches 0 or more non-comma characters.
  3. Body - Matches 'Body'.
  4. [^,]+ - Matches 1 or more non-comma characters.
  5. (?=,) - Positive lookahead assertion that the string must be followed by a ','.

If you assume that there is always a space following the initial comma and you do not want to match that, then use:

(?&lt;=, )[^,]*Body[^,]+(?=,)

If you assume that there must be a space following the initial comma and you do want to match that, then use:

(?&lt;=,) [^,]*Body[^,]+(?=,)

huangapple
  • 本文由 发表于 2020年8月1日 15:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63202800.html
匿名

发表评论

匿名网友

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

确定