Guys I hate Regex and I suck at writing.
I have a string that is space separated and contains several codes that I need to pull out. Each code is marked by beginning with a capital letter and ending with a number. The code is only two digits.
I’m trying to create an array of strings from the initial string and I can’t get the regular expression right.
Here is what I have
String[] test = Regex.Split(originalText, "([a-zA-Z0-9]{2})");
I also tried:
String[] test = Regex.Split(originalText, "([A-Z]{1}[0-9]{1})");
I don’t have any experience with Regex as I try to avoid writing them whenever possible.
Anyone have any suggestions?
Example input:
AA2410 F7 A4 Y7 B7 A 0715 0836 E0.M80
I need to pull out F7, A4, B7. E0 should be ignored.
You want to collect the results, not split on them, right?
should do this. The
\bs are word boundaries, making sure that only entire strings (likeA1) are extracted, not substrings (like theA1inTWA101).If you also need to exclude “words” with non-word characters in them (like
E0.M80in your comment), you need to define your own word boundary, for example:Now
A1only matches when surrounded by whitespace (or start/end-of-string positions).Explanation:
If you also need to find non-ASCII letters/digits, you can use
instead of
[A-Z][0-9]. This finds all uppercase Unicode letters and Unicode digits (likeÄ٣), but I guess that’s not really what you’re after, is it?