如何从REGEX中的字符串中排除2个字母字符

时间:2022-11-29 10:46:18

I am trying to find the REGEX for this particular product code.

我正在尝试为此特定产品代码找到REGEX。

Description:

  1. A valid product code must begin by a one of two digit number.
  2. 有效的产品代码必须以两位数字之一开头。

  3. The number can't begin with 0
  4. 该数字不能以0开头

  5. The number must be followed by a hyphen -
  6. 该数字必须后跟连字符 -

  7. The hyphen is followed by a sequence of upper case letters conforming to the patterns AAA and BBB
  8. 连字符后跟一系列符合模式AAA和BBB的大写字母

  9. AAA: string of any three upper case alphabetic characters except "I" and "O"
  10. AAA:除“I”和“O”之外的任何三个大写字母字符的字符串

  11. BBB string of any three upper case alphabetic characters in the range "A" to "D"
  12. “A”到“D”范围内任意三个大写字母字符的BBB字符串

Examples:

  • N-AAA

  • NN-AAA

  • NN-BBB

  • NN-AAABBB

My problem is with the point 5) I mentioned. My solution so far is:

我的问题在于我提到的第5点。到目前为止我的解决方案是

^[1-9]?[0-9]-[A-Z][^IO]{3}?[A-D]{3}$

I am not sure about the part that I highlighted in my RE. I am looking to know if my solution is correct and if it is not, I would like to know the answer and the reasoning behind it.

我不确定我在RE中突出显示的部分。我想知道我的解决方案是否正确,如果不是,我想知道答案及其背后的原因。

Thanks,

2 个解决方案

#1


0  

You have to use Negative Lookahead (?!([I]|[O])) in your Regex. Please check this:

您必须在正则表达式中使用否定先行(?!([I] | [O]))。请检查一下:

^[1-9]{1}[0-9]+\-(?!([I]|[O]))[A-Z]{3}[A-D]{3}

Old Update:

^[1-9]{1}[0-9]+\-((?!([I]|[O]))[A-Z]{3}[A-D]{3}|(?!([I]|[O]))[A-Z]{3}|[A-D]{3})

Updated regex:

^[1-9]{1}(|[0-9])\-([A-HJ-NP-Z]{3}[A-D]{3}|[A-HJ-NP-Z]{3}|[A-D]{3})

^[1-9]{1} Start with 1 to 9

^ [1-9] {1}从1到9开始

(|[0-9]) N or NN

(| [0-9])N或NN

\- hyphen (-)

\ - 连字符( - )

[A-HJ-NP-Z] Avoid I and O

[A-HJ-NP-Z]避免I和O.

([A-HJ-NP-Z]{3}[A-D]{3}|[A-HJ-NP-Z]{3}|[A-D]{3}) AAABBB or AAA or BBB

([A-HJ-NP-Z] {3} [A-D] {3} | [A-HJ-NP-Z] {3} | [A-D] {3})AAABBB或AAA或BBB

You can check it in Regex101.

你可以在Regex101中查看它。

Check my final update in Regex.

检查我在Regex中的最终更新。

#2


0  

You may use something like:

您可以使用以下内容:

^[1-9]\d?-(?:[A-HJ-NP-Z]{3}(?:[A-D]{3})?|(?:[A-D]{3}))$

Regex Explanation and Demo

正则表达式解释和演示

#1


0  

You have to use Negative Lookahead (?!([I]|[O])) in your Regex. Please check this:

您必须在正则表达式中使用否定先行(?!([I] | [O]))。请检查一下:

^[1-9]{1}[0-9]+\-(?!([I]|[O]))[A-Z]{3}[A-D]{3}

Old Update:

^[1-9]{1}[0-9]+\-((?!([I]|[O]))[A-Z]{3}[A-D]{3}|(?!([I]|[O]))[A-Z]{3}|[A-D]{3})

Updated regex:

^[1-9]{1}(|[0-9])\-([A-HJ-NP-Z]{3}[A-D]{3}|[A-HJ-NP-Z]{3}|[A-D]{3})

^[1-9]{1} Start with 1 to 9

^ [1-9] {1}从1到9开始

(|[0-9]) N or NN

(| [0-9])N或NN

\- hyphen (-)

\ - 连字符( - )

[A-HJ-NP-Z] Avoid I and O

[A-HJ-NP-Z]避免I和O.

([A-HJ-NP-Z]{3}[A-D]{3}|[A-HJ-NP-Z]{3}|[A-D]{3}) AAABBB or AAA or BBB

([A-HJ-NP-Z] {3} [A-D] {3} | [A-HJ-NP-Z] {3} | [A-D] {3})AAABBB或AAA或BBB

You can check it in Regex101.

你可以在Regex101中查看它。

Check my final update in Regex.

检查我在Regex中的最终更新。

#2


0  

You may use something like:

您可以使用以下内容:

^[1-9]\d?-(?:[A-HJ-NP-Z]{3}(?:[A-D]{3})?|(?:[A-D]{3}))$

Regex Explanation and Demo

正则表达式解释和演示