Home  >  Article  >  Backend Development  >  Need a regex to validate "earn an extra 20%" or "earn an extra 20%"

Need a regex to validate "earn an extra 20%" or "earn an extra 20%"

王林
王林forward
2024-02-09 13:33:09497browse

需要一个正则表达式来验证“赚取额外 20%”或“赚取额外 20%”

php editor Xiaoxin will introduce you to a regular expression in this article, used to verify "earn extra 20%" or "earn extra 20%". Regular expressions are a powerful text matching tool that can be used to validate and extract strings in a specific format. This regular expression can help us find strings that meet specific requirements in a piece of text and process them accordingly. In this article, we will explain the composition and use of this regular expression in detail to help everyone better understand and apply it.

Question content

Need a regular expression to verify the string mentioned below.

'Earn extra 20%' - valid

'Earn 20% extra' - valid

'earn extra 20%' - valid

'earn 20% extra' - valid

'Earn 20' - valid

'Earn ' - Invalid

'20' - Invalid

'Earn ^&' - Invalid

No other special characters should be allowed

- Using golang 1.19

Workaround

Use (?i) (case-insensitive) alternation of flags and optional terms (instead of using go Lookaround not supported):

(?i)earn (\d+%?( extra)?|extra \d+%?)

See Live Regular Expression Demo.

See Live Demo.

The above is the detailed content of Need a regex to validate "earn an extra 20%" or "earn an extra 20%". For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete