简体   繁体   English

使用正则表达式提取单词

[英]Extract words using regex

I have a string query and I want to match and extract specif words. 我有一个字符串查询,我想匹配并提取特定单词。 when I have this word word.EXACT extract the container of this word 当我有这个词word.EXACT提取这个词的容器时

like MESH.EXACT("blood glucose monitoring") extract the word "blood glucose monitoring" MESH.EXACT("blood glucose monitoring")提取单词"blood glucose monitoring"

  • words.exact("N-Words") -> result "N-Words" words.exact(“ N-Words”)->结果"N-Words"
  • words.exact(N-Words) -> result N-Words words.exact(N-Words)->结果N-Words

Query_Input= (EMB.EXACT("insulin treatment")) and (MESH.EXACT("blood glucose monitoring")) OR "Self-Monitoring of Blood Glucose”

the output needs to be like that 输出需要像这样

Query_out= "insulin treatment" "blood glucose monitoring" "Self-Monitoring of Blood Glucose” Query_out = "insulin treatment" "blood glucose monitoring" "Self-Monitoring of Blood Glucose”

this Demo has my regexp and my regex : https://regex101.com/r/rqpmXr/15 这个演示有我的正则表达式和我的正则表达式: https : //regex101.com/r/rqpmXr/15

You could do: 您可以这样做:

(?<=\w\.EXACT\()[^)]+

see the regex demo . 参见正则表达式演示 Match any char that is not a closing parenthesis, [^)]+ , only when preceded by \\w\\.EXACT( . 仅当在\\w\\.EXACT(之前[^)]+时,才\\w\\.EXACT(括号[^)]+任何字符。

If you want a substitution you could capture the above match and use \\1 (note the trailing space) for the repacement: 如果您想要替换,则可以捕获上面的匹配,并使用\\1 (请注意尾随空格)进行替换:

.*(?<=\w\.EXACT\()([^)]+).*\n|.*

as shown here: https://regex101.com/r/BS3nwr/4 如此处所示: https//regex101.com/r/BS3nwr/4

Edit: As was brought to my attention in one of the comments, look-behinds ( ?<= ) are not supported in some web browsers so you could use (note this regex is slower (requires more steps) than the previous one): 编辑:正如在其中一条评论中引起我注意的那样,某些Web浏览器不支持向后看( ?<= ),因此您可以使用(请注意,此regex比上一个更慢(需要更多步骤)):

\w+\.EXACT\(([^)]+).*\n|.*?

You may use 您可以使用

/\w+\.EXACT\(([^)]*)\)/g

and replace with $1 , placeholder holding Group 1 value. 并替换为$1 (占组1值的占位符)。 See the regex demo . 参见regex演示

Pattern details 图案细节

  • \\w+ - 1 or more word chars \\w+ -1个或多个字字符
  • \\.EXACT\\( - a literal .EXACT( substring \\.EXACT\\( -文字.EXACT(子字符串
  • ([^)]*) - Group 1: any 0+ chars other than ) (you may use [^()]* in case you need to make sure you are staying within 1 set of (...) ) ([^)]*) -第1组:除)之外的任何0+个字符(如果需要确保您停留在(...) 1套内,则可以使用[^()]*
  • \\) - a ) char. \\) -a )字符。

See the JS demo: 参见JS演示:

 var s = 'MESH.EXACT("blood glucose monitoring") words tt.EXACT("blood glucose monitoring") '; var rx = /\\w+\\.EXACT\\(([^)]*)\\)/g; document.querySelector("#result").innerHTML = s.replace(rx, "$1"); 
 <div id="result" /> 

Hi I believe this would work : 嗨,我相信这会起作用:

.EXACT\\((.*?)\\)

Working Example : https://regex101.com/r/uQj2vv/2/ 工作示例: https : //regex101.com/r/uQj2vv/2/

Here is an executable Javascript example which extracts the output you specified from the input you specified: 这是一个可执行的Javascript示例,该示例从指定的输入中提取指定的输出:

 let input = "(EMB.EXACT(\\"insulin treatment\\")) and (MESH.EXACT(\\"blood glucose monitoring\\")) OR \\"Self-Monitoring of Blood Glucose\\""; let re = /(?:EXACT\\(("[^"]+")\\)|OR\\s*("[^"]+"))/g; let Query_out = []; while ((match = re.exec(input)) !== null) { Query_out.push(match[1] ? match[1] : match[2]); } console.log(Query_out.join(" ")); 

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

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