[英]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。匹配条件有两个:
So what I mean is if I'm given DTTFDD as substring other proper matches would be:所以我的意思是,如果我得到 DTTFDD 作为 substring 其他适当的匹配将是:
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}
[DX]-?
- A literal "D" or "X" followed by an optional hyphen. (?:
- Open non-capture group: (?:
- 打开非捕获组:
[TX]-?
- A literal "T" or "X" followed by an optional hyphen. ){2}
- Close non-capture group and match twice. ){2}
- 关闭非捕获组并匹配两次。[FX]-?
- A literal "F" or "X" followed by an optional hyphen. (?:
- Open non-capture group: (?:
- 打开非捕获组:
[DX]-?
- A literal "D" or "X" followed by an optional hyphen. ){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.