0

Regex in Java is \A[a-zA-Z][\w._-/&&[^()]]*\z. Have never coded in Python/am not really familiar with it, and I can't figure out how to translate this over. Thank you.

ctwheels
  • 21,901
  • 9
  • 42
  • 77
  • What is this regex for? – andreoss Jun 30 '20 at 15:04
  • You can refer to this site, https://regexr.com/ – Sabareesh Muralidharan Jun 30 '20 at 15:04
  • 2
    What happened when you tried this regular expressiohn in Python? Any errors? – Matthias Jun 30 '20 at 15:05
  • This regex won't work in Java. Where did you get it from? – Arvind Kumar Avinash Jun 30 '20 at 15:07
  • @ArvindKumarAvinash A work wikipedia article. – alexkirigiya Jun 30 '20 at 15:11
  • possible differences from Python: `\A` in Java regex means "beginning of input", `\z` means "end of input"... Python regex has `\A`, same meaning, but only `\Z` (seems same as `\Z` in Java, slightly different meaning to `\z` – Anentropic Jun 30 '20 at 15:11
  • https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html https://docs.python.org/3/library/re.html – Anentropic Jun 30 '20 at 15:12
  • Errors I get are: PEP 8: W605 invalid escape sequence '\A', '\w', '\z' – alexkirigiya Jun 30 '20 at 15:12
  • @Anentropic thank you, this is very helpful. – alexkirigiya Jun 30 '20 at 15:12
  • This can be the converted one. `(?m)^[a-zA-Z]((?![()])[-\w./])*$`.According to what I figured out; your regex tests a string which starts with the letter and contain `-\w./` zero or more times but does not contain `()`. –  Jun 30 '20 at 15:15
  • 1
    @Mandy8055 i could not transfer through the computer the amount of appreciation i have for you right now – alexkirigiya Jun 30 '20 at 15:26
  • No worries @alexkirigiya....I'm glad I could help =) –  Jun 30 '20 at 15:28
  • 1
    That's not even a valid Java regex because the range `_-/` isn't a valid range – ctwheels Jun 30 '20 at 15:47
  • 1
    @alexkirigiya the errors you're getting for escaped characters likely has to do with not using the raw string literal: `r'regex goes here'` – ctwheels Jun 30 '20 at 15:49
  • 1
    @ctwheels; I guess [**intersection class**](https://docs.oracle.com/javase/tutorial/essential/regex/char_classes.html) is not supported in python regex flavor too. Please correct me if I'm wrong –  Jun 30 '20 at 16:04
  • @Mandy8055 it's not. I can't provide an ideal regex without a proper range either (OP's regex isn't valid) – ctwheels Jun 30 '20 at 16:06
  • in this case the intersection class is not required: the `&&[^()]` in the java regex means "everything in the outer class _except_ for `(` and `)`"... but the parens are not captured by the outer character class (they are not matched by `\w`) so you could safely omit the `&&[^()]` part – Anentropic Jul 01 '20 at 17:49
  • assuming a literal `-` was intended rather than a char range then: `r"\A[a-zA-Z][\w._\-/]*\Z"` works, but note the minor semantic difference between Java's `\z` and Python's `\Z` – Anentropic Jul 01 '20 at 17:54
  • (escaping the `-` as `\-` is needed in the Java version too, as written currently it is invalid in Java because `_-/` means a char range, but is not a valid one... try it here http://www.regexplanet.com/cookbook/ahJzfnJlZ2V4cGxhbmV0LWhyZHNyEwsSBlJlY2lwZRiAgIDSvseLCgw/index.html) – Anentropic Jul 01 '20 at 17:58

0 Answers0