简体   繁体   English

如何提取两个单词之间的句子并排除特殊字符

[英]How to extract sentence in between two words and exclude special characters

I have the following sentence from a json string and i want to parse only the words for the "value" part of the string ie Service Copy我有来自 json 字符串的以下句子,我只想解析字符串的“值”部分的单词,即服务复制

"customtext_230216":"self":"https://jsonapi.com/restsv/api/13/customtext/11233","value":"Service Copy","id":"11211"}

After several searches on SO i was able to get the regex expression在 SO 上进行几次搜索后,我能够获得正则表达式

\b(?<=value)(.*?)(?=\s*id)

and the result is结果是

":"Service Copy","

How can I enhance this so that the special characters are not captured expected result:如何增强这一点,以便不捕获特殊字符的预期结果:

Service Copy

If you want to stick with regex:如果你想坚持使用正则表达式:

(?<=value\":\")(.*?)(?=\s*\",\"id)

Basically took out the word boundary in the beginning and defined the special characters (quotes, colons, etc) outside of the capturing group.基本上一开始就去掉了单词边界,定义了捕获组之外的特殊字符(引号、冒号等)。

However, there are better ways to extract values from JSON and I encourage you to look at those as this could potentially be a brittle solution.但是,有更好的方法可以从 JSON 中提取值,我鼓励您查看这些方法,因为这可能是一个脆弱的解决方案。

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

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