import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?P<preceeding>^(?P<whitespace>(?:[ ]{4}|\\t)+)(?!self\\.)(?P<attrprefix>[a-z0-9_\\.]{1,})?(?P<clsattrname>[A-Z_]{1,}): )(?P<annotation>(?!ClassVar)(?:[A-Za-z0-9_]+\\.*)+(?:(?:(?:\\[?[A-Za-z0-9_]+(?:, | \\| )?)+(?:\\.{3})?)*(?:\\]*))*)+(?P<remainder>.*?$[\\r\\n])";
final String string = "# Regex:\n"
+ "regex_str = r\"(?P<preceeding>^(?P<whitespace>(?:[ ]{4}|\\t)+)(?!self\\.)(?P<attrprefix>[a-z0-9_\\.]{1,})?(?P<clsattrname>[A-Z_]{1,}): )(?P<annotation>(?!ClassVar)(?:[A-Za-z0-9_]+\\.*)+(?:(?:(?:\\[?[A-Za-z0-9_]+(?:, | \\| )?)+(?:\\.{3})?)*(?:\\]*))*)+(?P<remainder>.*?$[\\r\\n])\"\n\n"
+ "#pycharm_compatible_version = (?<preceeding>^(?<whitespace>(?:[ ]{4}|\\t)+)(?!self\\.)(?<attrprefix>[a-z0-9_\\.]{1,})?(?<clsattrname>[A-Z_]{1,}): )(?<annotation>(?!ClassVar)(?:[A-Za-z0-9_]+\\.*)+(?:(?:(?:\\[?[A-Za-z0-9_]+(?:, | \\| )?)+(?:\\.{3})?)*(?:\\]*))*)+(?<remainder>.*?$[\\r\\n])\n\n"
+ "# WARNINGS\n"
+ "# - Top-level functions and local constants are capture as well if they are written in SCREAMING_SNAKE style!\n"
+ "#Assumes:\n"
+ "# - Class variables are named according to SCREAMING_SNAKE convention.\n"
+ "# - Functions and attributes are named according to underscored_lowercase convention.\n"
+ "# - Indent -> 4 spaces or 1 tab per level.\n"
+ "# Notes:\n"
+ "# - Unannotated variables are skipped\n"
+ "# - In PyCharm IDE regex replace; replace occurences of ?P< with ?<\n\n"
+ "import pathlib, os\n"
+ "TOPLEVEL_CONSTANT: str = 'nomatch' # No match\n\n"
+ "class TestAttr:\n"
+ " class TestAttrNested:\n"
+ " underscore_test = 'asdf'\n\n"
+ "class SomeClass(ParentClass, SomeMixin):\n"
+ " FOO_CLS_ATTR: Union[str, int] = 'foo' # Regex should match\n"
+ " BAR_CLS_ATTR: Sequence[tuple[int, ...]] = ((1, 2), (3, 4)) # Regex should match\n"
+ " ANNOTATION_AS_ATTR_ATTR: TestAttr.TestAttrNested.underscore_test = 'asdf' # Regex should match\n"
+ " FIZZ_CLS_ATTR: ClassVar[bool] = True # No match\n"
+ " BUZZ_CLS_ATTR = 10 # No match (not annotated)\n"
+ " \n"
+ " def __init__(self):\n"
+ " local_instance_attribute: float = 1337.420 # No match\n"
+ " self.instance_attribute: str = 'NO_MATCH_PLEASE'\n\n"
+ " class NestedClass:\n"
+ " NESTED_ATTR: str = 'nested_attribute'\n"
+ " \n"
+ " @classmethod\n"
+ " def nested_method(cls):\n"
+ " cls.NESTED_ATTR: str = 'should_match' # Regex should match\n"
+ " print(\"Nested lvl 1!\")\n"
+ " def arbitrarilyy_nested_func(obj):\n"
+ " obj.a.b.c.d.ARBITRARILY_NESTED_ATTR: str = 'nested' # Regex should match\n\n"
+ " @classmethod\n"
+ " def some_method(cls):\n"
+ " cls.FOO_CLS_ATTR: str = 'foo' # Regex should match\n"
+ " cls.instance_attribute.OTHER_CLASATTR: str = 'foo' # Regex should match\n"
+ " print(cls.FOO_CLS_ATTR) # No Match\n\n"
+ "def toplevel_func(arg1: int, CONST_ARG: int = 10) -> int: # No match\n"
+ " MULTIPLIER: float = 6.9\n"
+ " # ^^ Warning! Constants in top-level functions are captured as well! \n"
+ " # ^^ Python doesn't support subpatterns AFAIK.\n"
+ " return arg1*MULTIPLIER\n\n"
+ "### --> after replacing by ${preceeding}ClassVar[${annotation}]${remainder} ###\n\n"
+ "class SomeClass(ParentClass, SomeMixin):\n"
+ " FOO_CLS_ATTR: ClassVar[str] = 'foo' # Regex matches\n"
+ " BAR_CLS_ATTR: ClassVar[Sequence[int]] = (1, 2, 3)\n"
+ " FIZZ_CLS_ATTR: ClassVar[bool] = True\n"
+ " BUZZ_CLS_ATTR = 10 # No match (not annotated)\n"
+ " instance_attribute: float = 1337.420\n\n"
+ " class NestedClass:\n"
+ " NESTED_ATTR: ClassVar[str] = 'nested_attribute'\n"
+ " \n"
+ " @classmethod\n"
+ " def nested_method(cls):\n"
+ " cls.NESTED_ATTR: ClassVar[str] = 'should_match'\n"
+ " print(\"Nested lvl 1!\")\n"
+ " def arbitrarilyy_nested_func(obj):\n"
+ " obj.a.b.c.d.ARBITRARILY_NESTED_ATTR: ClassVar[str] = 'nested'\n\n"
+ " @classmethod\n"
+ " def some_method(cls):\n"
+ " cls.FOO_CLS_ATTR: ClassVar[str] = 'foo' # Regex matches\n"
+ " cls.instance_attribute.OTHER_CLASATTR: ClassVar[str] = 'foo'\n"
+ " print(cls.FOO_CLS_ATTR) # No Match\n\n"
+ "def toplevel_func(arg1: int, CONST_ARG: int = 10) -> int\n"
+ " MULTIPLIER: ClassVar[float] = 6.9 \n"
+ " # ^^ ERRONEOUSLY ADJUSTED...\n"
+ " # ^^ Warning! Constants in top-level functions are captured as well! \n"
+ " # ^^ Python doesn't support subpatterns AFAIK.\n"
+ " return arg1*MULTIPLIER";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html