英文:
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+
答案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:
(?<=, )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.
答案2
得分: 1
(?<=,)
查找正向后面有逗号的部分
[\s\w]*
匹配任意数量的空白字符或字母数字字符
Body
匹配 Body
后跟一个空格字符
.*?
懒惰地匹配任何字符
(?=,)
查找正向前面有逗号的部分
英文:
You can use the regex, (?<=,)[\s\w]*Body .*?(?=,)
Check this for a demo.
Explanation:
(?<=,)
is looking behind positively for a comma[\s\w]*
matches any number of whitespace character or a word characterBody
matchesBody
followed by a space character.*?
matches any character lazily(?=,)
is looking ahead positively for a comma
答案3
得分: 0
(?<=,)
- 正向回顾断言,字符串必须在逗号之前。[^,]*
- 匹配零个或多个非逗号字符。Body
- 匹配 'Body'。[^,]+
- 匹配一个或多个非逗号字符。(?=,)
- 正向预查断言,字符串必须在逗号之后。
如果您假设初始逗号后始终有一个空格,并且您 不 想匹配它,则使用:
(?<=, )[^,]*Body[^,]+(?=,)
如果您假设初始逗号后 必须 有一个空格,并且您 希望 匹配它,则使用:
(?<=,) [^,]*Body[^,]+(?=,)
英文:
(?<=,)[^,]*Body[^,]+(?=,)
(?<=,)
- Positive lookbehind assertion that the string must be preceded by a ','.[^,]*
- Matches 0 or more non-comma characters.Body
- Matches 'Body'.[^,]+
- Matches 1 or more non-comma characters.(?=,)
- 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:
(?<=, )[^,]*Body[^,]+(?=,)
If you assume that there must be a space following the initial comma and you do want to match that, then use:
(?<=,) [^,]*Body[^,]+(?=,)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论