regex 创建正则表达式以基于条件替换单词[已关闭]

o8x7eapl  于 2023-08-08  发布在  其他
关注(0)|答案(3)|浏览(94)

已关闭。此问题需要更多focused。它目前不接受回答。
**希望改进此问题?**更新问题,使其仅针对editing this post的一个问题。

20天前关门了。
Improve this question
我有一个文件,里面有一些数据。我只需要匹配Title=""中的文本的正则表达式,但忽略文本是否在方括号内,而不是其他的。Link、BreakType等
我想要匹配的词是颜色。

<TocEntry
      Title="Color" - Highlight this text Color
Title=["Color"] - Ignore  this text Color
      Link="/Content/Color/" - ignore this text Color
      BreakType="Color" - ignore this text Color
      StartSection="false"
      PageNumberReset="Color"> - ignore this text Color
</TocEntry>
<TocEntry
      Title="Colord"> - ignore this text Colord
      //there could be Link="abc", BreakType="abc" here aswell
</TocEntry>
<TocEntry
      Title="dColord"> - ignore this text dColord
</TocEntry>

<TocEntry
      Title="my fav Color is red"> - Highlight this text Color
Title=["my fav Color is red"]> -  - ignore this text Color
</TocEntry>
<TocEntry
      Title="my fav 
Color 
is red"> - Highlight this text Color
</TocEntry>

字符串

anhgbhbe

anhgbhbe1#

如果您只想在这些示例中匹配单词Color,则此正则表达式将起作用(运行示例以查看实际情况)/(?<=Title="[^"]*)\bColor\b(?=[^"]*")/g
故障:(?<=Title="[^"]*):正向后查找,确保匹配前面有但不包括Title=”,后面有0个或多个非”字符
\bColor\b:匹配单词Color,但仅当它两边都有边界(不是数字或字母)时
(?=[^"]*"):积极的前瞻,确保匹配后面是但不包括任何数量的非“字符后面是一个”

const input = `<TocEntry
      Title="Color" - Highlight this text Color
Title=["Color"] - Ignore  this text Color
      Link="/Content/Color/" - ignore this text Color
      BreakType="Color" - ignore this text Color
      StartSection="false"
      PageNumberReset="Color"> - ignore this text Color
</TocEntry>
<TocEntry
      Title="Colord"> - ignore this text Colord
      //there could be Link="abc", BreakType="abc" here aswell
</TocEntry>
<TocEntry
      Title="dColord"> - ignore this text dColord
</TocEntry>

<TocEntry
      Title="my fav Color is red"> - Highlight this text Color
Title=["my fav Color is red"]> -  - ignore this text Color
</TocEntry>
<TocEntry
      Title="my fav 
Color 
is red"> - Highlight this text Color
</TocEntry>`

const matches = input.match(/(?<=Title="[^"]*)\bColor\b(?=[^"]*")/g)

console.log(matches)

console.log(input.replace(/(?<=Title="[^"]*)\bColor\b(?=[^"]*")/g, '[MATCHED AND REPLACED]'))

字符串

gajydyqb

gajydyqb2#

这个正则表达式使用了单词边界

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

字符串
确保为不区分大小写设置“i”标志
工作示例:https://regex101.com/r/xlDpc2/1

xwbd5t1u

xwbd5t1u3#

试试这个

data="""<TocEntry
      Title="Color" - Highlight this text Color
Title=["Color"] - Ignore  this text Color
      Link="/Content/Color/" - ignore this text Color
      BreakType="Color" - ignore this text Color
      StartSection="false"
      PageNumberReset="Color"> - ignore this text Color
</TocEntry>
<TocEntry
      Title="Colord"> - ignore this text Colord
      //there could be Link="abc", BreakType="abc" here aswell
</TocEntry>
<TocEntry
      Title="dColord"> - ignore this text dColord
</TocEntry>

<TocEntry
      Title="my fav Color is red"> - Highlight this text Color
Title=["my fav Color is red"]> -  - ignore this text Color
</TocEntry>
<TocEntry
      Title="my fav 
Color 
is red"> - Highlight this text Color
</TocEntry>"""
def get_title_value(data):
     return re.findall(r'Title="(.*?\bColor\b.*?)"', data, flags=re.IGNORECASE)

print(get_title_value(data))

字符串
产出:

['Color', 'my fav Color is red']

相关问题