-3

I am python programmer and always played regular expressions in python but recently I have been asked to use java flavor regular expression, does mean I have to write application in java. So just checking is there a way to use java regex in python? Thanks

9
  • Regex does differ a bit across languages, but the core functions remain the same. For more details stackoverflow.com/a/10492382/4983469 Commented Aug 3, 2021 at 12:40
  • Thanks for the checking. Yes I know different across the languages but how do I get java flavor in python? Commented Aug 3, 2021 at 12:59
  • check the link. Commented Aug 3, 2021 at 13:20
  • No point of adding negative mark on a valid question. I have already looked at the link and it does not work for my regex. Example I have look behind regex (?:(?<!.?[-:0-9\/] ?) the optional . and space does not work in python Commented Aug 3, 2021 at 13:32
  • No, not easily or directly. What would be the point? Can you speak Spanish in English? Maybe I am missing the point. Commented Aug 4, 2021 at 0:59

1 Answer 1

0

https://www.py4j.org

Below java program need to be run first

import py4j.GatewayServer;

public class AdditionApplication {

  public int addition(int first, int second) {
    return first + second;
  }

  public static void main(String[] args) {
    AdditionApplication app = new AdditionApplication();
    // app is now the gateway.entry_point
    GatewayServer server = new GatewayServer(app);
    server.start();
  }
}

Below is the way to access java addition method in python

from py4j.java_gateway import JavaGateway
gateway = JavaGateway()
gateway.entry_point.addition(10, 10)
Sign up to request clarification or add additional context in comments.

2 Comments

Per, py4j.org, the port #s used are 25332-25334, but per iana.org/assignments/service-names-port-numbers/…, those port # are unassigned.
They may not be but default ports. These ports can be changed py4j.org/py4j_java_gateway.html#module-py4j.java_gateway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.