英文:
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 -->
<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>
<!-- end snippet -->
答案1
得分: 0
这个正则表达式通过单词边界实现功能:
Title="([^"](\bcolor\b)[^"])"
确保设置"i"标志以进行大小写不敏感匹配。
工作示例:https://regex101.com/r/xlDpc2/1
英文:
This regex does the trick using a word boundary
Title="([^"]*(\bcolor\b)[^"]*)"
make sure to set the "i" flag for case insensitivity
Working Ex: https://regex101.com/r/xlDpc2/1
答案2
得分: 0
/(?<=Title="[^"]*)\bColor\b(?=[^"]*")/g
的解析:
(?<=Title="[^"]*)
: 正向回顾后断言,确保匹配的内容之前有但不包括 Title=" 后面跟着 0 或更多个非-" 字符。\bColor\b
: 匹配单词 "Color",但只有在两边有边界(不是数字或字母)时才匹配。(?=[^"]*")
: 正向前瞻,确保匹配的内容之后有但不包括任意数量的非-" 字符,然后跟着一个 "。
这个正则表达式的作用是匹配包含在 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)
/(?<=Title="[^"]*)\bColor\b(?=[^"]*")/g
breakdown:
(?<=Title="[^"]*)
: 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)
(?=[^"]*")
: 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 = `<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]'))
<!-- end snippet -->
答案3
得分: 0
def get_title_value(data):
return re.findall(r'Title="(.*?\bColor\b.*?)"', data, flags=re.IGNORECASE)
print(get_title_value(data))
output:
['Color', 'my fav Color is red']
英文:
try this
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))
output:
['Color', 'my fav Color is red']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论