简体   繁体   中英

Regular expression that Must have at least one letter

I have a case where I am using a queue of regular expressions to filter out specific items in an Observer pattern. The filter will place the values in specific controls based on their values. However 1 of the controls pattern is that it can accept ANY ASCII Character. Let me list the filters in their order with the RegEx

Column         Rule                        Regex
Receiving     7 DIGITS                     @"^[1-9]([0-9]{6}$)"   --->Works
Count         2 digits, no leading 0       @"^[1-9]([0-9]{0,1})$" --->Works
Producer      any ASCII char.              @".*"                  --->too broad
              MUST contain a letter

Is there a regular expression that will accept any set of ASCII characters, but 1 of them MUST be a letter (upper or lower case)? @"^(?=.*[A-Za-z])$" -->Didn't work

examples that would need to go into expression

  • 123 red
  • red
  • 123 red123
  • red - 123

red

If you want to match the whole rang of ASCII chars you may use

@"^(?=[^A-Za-z]*[A-Za-z])[\x00-\x7F]*$"

If only printable chars are allowed use

@"^(?=[^A-Za-z]*[A-Za-z])[ -~]*$"

Note the (?=[^A-Za-z]*[A-Za-z]) positive lookahead is located right after ^ , that is, it is only triggered at the start of a string. It requires an ASCII letter after any 0 or more chars other than an ASCII letter.

Your ^(?=.*[A-Za-z])$ pattern did not work because you wanted to match an empty string ( ^$ ) that contains (?=...) at least one ASCII letter ( [A-Za-z] ) after any 0+ chars other than newline ( .* ).

How about ^.*[a-zA-Z]+.*$ ?

Between start and end of line, accept any number of any characters, then at least one az/AZ character, then again any number of any characters.

You could try [A-Za-z]+ .

It matches when there is at least one letter. You want something more specific?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM