使用正则表达式根据条件替换单词。

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

Creating a regex to replace word based on condition

问题

Title="Color" - 匹配此文本 Color
Title=["Color"] - 忽略此文本 Color
Link="/Content/Color/" - 忽略此文本 Color
BreakType="Color" - 忽略此文本 Color
StartSection="false"
PageNumberReset="Color" - 忽略此文本 Color

Title="my fav Color is red" - 匹配此文本 Color
Title=["my fav Color is red"] - 忽略此文本 Color

Title="my fav
Color
is red" - 匹配此文本 Color

英文:

I have a file, with some data.
I need the regex that matches a text only within the Title=“”, but ignore if the text is within square brackets, and not others for ex. Link, BreakType etc

The word i want to match is Color.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;TocEntry
      Title=&quot;Color&quot; - Highlight this text Color
Title=[&quot;Color&quot;] - Ignore  this text Color
      Link=&quot;/Content/Color/&quot; - ignore this text Color
      BreakType=&quot;Color&quot; - ignore this text Color
      StartSection=&quot;false&quot;
      PageNumberReset=&quot;Color&quot;&gt; - ignore this text Color
&lt;/TocEntry&gt;
&lt;TocEntry
      Title=&quot;Colord&quot;&gt; - ignore this text Colord
      //there could be Link=&quot;abc&quot;, BreakType=&quot;abc&quot; here aswell
&lt;/TocEntry&gt;
&lt;TocEntry
      Title=&quot;dColord&quot;&gt; - ignore this text dColord
&lt;/TocEntry&gt;

&lt;TocEntry
      Title=&quot;my fav Color is red&quot;&gt; - Highlight this text Color
Title=[&quot;my fav Color is red&quot;]&gt; -  - ignore this text Color
&lt;/TocEntry&gt;
&lt;TocEntry
      Title=&quot;my fav 
Color 
is red&quot;&gt; - Highlight this text Color
&lt;/TocEntry&gt;

<!-- end snippet -->

答案1

得分: 0

这个正则表达式通过单词边界实现功能:

Title="([^"](\bcolor\b)[^"])"

确保设置"i"标志以进行大小写不敏感匹配。

工作示例:https://regex101.com/r/xlDpc2/1

英文:

This regex does the trick using a word boundary

Title=&quot;([^&quot;]*(\bcolor\b)[^&quot;]*)&quot;

make sure to set the "i" flag for case insensitivity

Working Ex: https://regex101.com/r/xlDpc2/1

答案2

得分: 0

/(?&lt;=Title=&quot;[^&quot;]*)\bColor\b(?=[^&quot;]*&quot;)/g 的解析:

  • (?&lt;=Title=&quot;[^&quot;]*): 正向回顾后断言,确保匹配的内容之前有但不包括 Title=" 后面跟着 0 或更多个非-" 字符。
  • \bColor\b: 匹配单词 "Color",但只有在两边有边界(不是数字或字母)时才匹配。
  • (?=[^&quot;]*&quot;): 正向前瞻,确保匹配的内容之后有但不包括任意数量的非-" 字符,然后跟着一个 "。

这个正则表达式的作用是匹配包含在 Title=" 和 " 之间的单词 "Color",但只有当 "Color" 是这两个边界之间的整个单词时才匹配。

请注意,由于我是一个文本模型,无法运行代码,因此我只提供了正则表达式的解析部分。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

If you're looking to only match the word Color in those instances, this regex will work (run sample to view in action)
/(?&lt;=Title=&quot;[^&quot;]*)\bColor\b(?=[^&quot;]*&quot;)/g

breakdown:
(?&lt;=Title=&quot;[^&quot;]*): positive lookbehind, ensure that the match is preceded by but does not include Title=" followed by 0 or more number of non-" characters

\bColor\b: match the word Color, but only if it has boundaries on both sides (not numbers or letters)

(?=[^&quot;]*&quot;): positive lookahead, ensure that the match is followed by but does not include any number of non-" characters followed by a single "

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = `&lt;TocEntry
      Title=&quot;Color&quot; - Highlight this text Color
Title=[&quot;Color&quot;] - Ignore  this text Color
      Link=&quot;/Content/Color/&quot; - ignore this text Color
      BreakType=&quot;Color&quot; - ignore this text Color
      StartSection=&quot;false&quot;
      PageNumberReset=&quot;Color&quot;&gt; - ignore this text Color
&lt;/TocEntry&gt;
&lt;TocEntry
      Title=&quot;Colord&quot;&gt; - ignore this text Colord
      //there could be Link=&quot;abc&quot;, BreakType=&quot;abc&quot; here aswell
&lt;/TocEntry&gt;
&lt;TocEntry
      Title=&quot;dColord&quot;&gt; - ignore this text dColord
&lt;/TocEntry&gt;

&lt;TocEntry
      Title=&quot;my fav Color is red&quot;&gt; - Highlight this text Color
Title=[&quot;my fav Color is red&quot;]&gt; -  - ignore this text Color
&lt;/TocEntry&gt;
&lt;TocEntry
      Title=&quot;my fav 
Color 
is red&quot;&gt; - Highlight this text Color
&lt;/TocEntry&gt;`

const matches = input.match(/(?&lt;=Title=&quot;[^&quot;]*)\bColor\b(?=[^&quot;]*&quot;)/g)

console.log(matches)

console.log(input.replace(/(?&lt;=Title=&quot;[^&quot;]*)\bColor\b(?=[^&quot;]*&quot;)/g, &#39;[MATCHED AND REPLACED]&#39;))

<!-- end snippet -->

答案3

得分: 0

def get_title_value(data):
     return re.findall(r'Title=&quot;(.*?\bColor\b.*?)&quot;', data, flags=re.IGNORECASE)

print(get_title_value(data))

output:

['Color', 'my fav Color is red']
英文:

try this

data=&quot;&quot;&quot;&lt;TocEntry
      Title=&quot;Color&quot; - Highlight this text Color
Title=[&quot;Color&quot;] - Ignore  this text Color
      Link=&quot;/Content/Color/&quot; - ignore this text Color
      BreakType=&quot;Color&quot; - ignore this text Color
      StartSection=&quot;false&quot;
      PageNumberReset=&quot;Color&quot;&gt; - ignore this text Color
&lt;/TocEntry&gt;
&lt;TocEntry
      Title=&quot;Colord&quot;&gt; - ignore this text Colord
      //there could be Link=&quot;abc&quot;, BreakType=&quot;abc&quot; here aswell
&lt;/TocEntry&gt;
&lt;TocEntry
      Title=&quot;dColord&quot;&gt; - ignore this text dColord
&lt;/TocEntry&gt;

&lt;TocEntry
      Title=&quot;my fav Color is red&quot;&gt; - Highlight this text Color
Title=[&quot;my fav Color is red&quot;]&gt; -  - ignore this text Color
&lt;/TocEntry&gt;
&lt;TocEntry
      Title=&quot;my fav 
Color 
is red&quot;&gt; - Highlight this text Color
&lt;/TocEntry&gt;&quot;&quot;&quot;
def get_title_value(data):
     return re.findall(r&#39;Title=&quot;(.*?\bColor\b.*?)&quot;&#39;, data, flags=re.IGNORECASE)

print(get_title_value(data))

output:

[&#39;Color&#39;, &#39;my fav Color is red&#39;]

huangapple
  • 本文由 发表于 2023年7月13日 22:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680259.html
匿名

发表评论

匿名网友

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

确定