Zam89
Zam89

Reputation: 143

Start with letter, must container letter and numbers

I trying for hours to find a regex which match this creteria:

Need to find a big text the following:

Here a text exampel: (need to match the "159C5F"

AT exempt acc to §§ 4 Nr. 1A UStG iVm 6 USTG
                                                                                                                            254,49/100L    977,23
159C5F Magnatec St-St 0W-30 D, 20L E4                                                                                                   0,00%
Commodity Code :

Here are a few of my tries:

(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]{6})
[0-9]{2}\S*[a-zA-Z0-9]{4}
[0-9]\S*[a-zA-Z0-9]{6}
[a-zA-Z0-9]{6}

Upvotes: 4

Views: 162

Answers (2)

The fourth bird
The fourth bird

Reputation: 163577

You might use word boundaries and a positive lookahead to assert the space at the end but not match it to match to get only 159C5F

\b\d{2}(?=[A-Z0-9]{4} )[A-Z0-9]*[A-Z][A-Z0-9]*\b
  • \b Word boundary
  • \d{2} Match 2 digits
  • (?= Positive lookahead, assert what is on the right is
    • [A-Z0-9]{4} Match 4 times any of the listed followed by a space
  • ) Close lookahead
  • [A-Z0-9]*[A-Z][A-Z0-9]* Match 0+ times A-Z0-9 with at least a single uppercase char
  • \b Word boundary

Regex demo

Upvotes: 2

41686d6564
41686d6564

Reputation: 19661

If I understand your requirements correctly, you may use something like this:

\b\d\d(?=\S*?[A-Z]\S*?\s)[A-Z0-9]{4}\s

Demo.

Some notes:

  • If you don't want to capture the space at the end, you may replace the final \s with a Lookahead (i.e., (?=\s)).

  • If you want to accept only the ASCII space character and no other whitespace characters, you may use [ ] instead of \s in both occurrences.

Upvotes: 4

Related Questions