简体   繁体   English

如何创建正则表达式,其中模式中的字符之间可以有特定的字符并且我有一个“通配符”字符?

[英]How to create regex where there can be specific char between characters in pattern and I have one “wildcard” char?

I want to create regex in python where I'm given a substring and I want to find it in my string.我想在 python 中创建正则表达式,我得到一个 substring 并且我想在我的字符串中找到它。 Characters in substring and my string are always either D, T or F. There are two conditions for match: substring 和我的字符串中的字符始终为 D、T 或 F。匹配条件有两个:

  1. After every character in given substring there can occur char '-' (I don't know how to approach this one especially)在给定 substring 中的每个字符之后,可能会出现 char '-' (我不知道如何特别处理这个)
  2. Every character can be either the character I'm looking at or 'X' so X is a "wildcard" (I know I can use '|' for that so it would be I believe ([DTF]|X))每个字符都可以是我正在查看的字符或“X”,因此 X 是“通配符”(我知道我可以使用“|”,所以我相信 ([DTF]|X))

So what I mean is if I'm given DTTFDD as substring other proper matches would be:所以我的意思是,如果我得到 DTTFDD 作为 substring 其他适当的匹配将是:

  • D - TTFDD D - TTFDD
  • D X TFDD D X TFDD

Edit: These matches can occur in bigger string such as FTDTTDFD D-TTFXD TFTFD编辑:这些匹配可能出现在更大的字符串中,例如 FTDTTDFD D-TTFXD TFTFD

How can I put all of this together?我怎样才能把所有这些放在一起?

Looks like you could try:看起来你可以尝试:

[DX]-?(?:[TX]-?){2}[FX]-?(?:[DX]-?){2}

See the online demo查看在线演示

  • [DX]-? - A literal "D" or "X" followed by an optional hyphen. - 文字“D”或“X”后跟可选的连字符。
  • (?: - Open non-capture group: (?: - 打开非捕获组:
    • [TX]-? - A literal "T" or "X" followed by an optional hyphen. - 文字“T”或“X”后跟可选的连字符。
    • ){2} - Close non-capture group and match twice. ){2} - 关闭非捕获组并匹配两次。
  • [FX]-? - A literal "F" or "X" followed by an optional hyphen. - 文字“F”或“X”后跟可选的连字符。
  • (?: - Open non-capture group: (?: - 打开非捕获组:
    • [DX]-? - A literal "D" or "X" followed by an optional hyphen. - 文字“D”或“X”后跟可选的连字符。
    • ){2} - Close non-capture group and match twice. ){2} - 关闭非捕获组并匹配两次。

A little less verbose without the non-capture groups:没有非捕获组的冗长一点:

[DX]-?[TX]-?[TX]-?[FX]-?[DX]-?[DX]-?

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

相关问题 Python正则表达式字符模式组 - Python regex char pattern group 如何增加一个字符? - How can I increment a char? RegEx用于在特定位置添加字符 - RegEx for adding a char in specific places 只有在特定字符出现之前才会匹配char(条件正则表达式) - regex matching char only if a specific char appeared before (conditional regex) Python Regex将所有换行符直接替换为后跟char的char - Python Regex replace all newline characters directly followed by a char with char 在特定模式匹配中使用正则表达式通配符 - using a regex wildcard within a specific pattern match RegEx-如何在字符串中的非字母字符和字母字符之间留空格 - RegEx- How to make a blank space between a non-alphabetic char and an alphabetic char in a string 如何在python中使用正则表达式获取字符串(数字和字符的混合)到最后一位? - How can I get a string(Mix of digit and char) up to last digit using regex in python? RegEx 在 (Number-Number)、(Number-char)、(Char-Special char) 和 (special char- char) 之间插入空格,除了 '\' - RegEx to insert spaces between (Number-Number), (Number-char), (Char-Special char) and (special char- char) except '\' 如何获得输入字符的位置? - How can I get the position of the entered char?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM