-1

I am having the below string.

Functions.substring(Functions.test(1,Functions.test("TestString1")),0.9,0,"TestString2",true)

In the above string, I want to replace Numbers(0 and 1 as in the above example) as Integer. And Float Values(Ex: 0.9) as Decimal and String(EX: "TestString1" and "TestString2") as String.

Result: Functions.substring(Functions.test(Integer,Functions.test(String)),Decimal,Integer,String,true)

I tried with replace function using below regex but it is not working.

decimal: ^[+-]?\d+(\.\d+)?$
number : ^-?(0|[1-9]\d*)(\d+)?$
String : ".*?"

Below is the java code:

public static void main(String[] args) {

                System.out.println("Functions.substring(Functions.test(1,Functions.test(\"TestString1\")),0.9,0,\"TestString2\",true)".replaceAll("^-?(0|[1-9]\\d*)(\\d+)?$", "Intger"));
    }
4
  • but it is not working is no error description. What is what you get? Commented Dec 4, 2019 at 11:20
  • First problem is that you can't use "^" and "$" because they mean beginning a end of a line which is the entire string in this case, but in your sample text you have text before and after the numbers you want to replace. Commented Dec 4, 2019 at 11:52
  • In this example, what is the expected output? Commented Dec 4, 2019 at 12:29
  • It appears that your intention is to identify and then replace certain values within a String using regular expressions in Java. Are other approaches (languages, tools) also possible for you? If so, then you should restate the problem using less specific implementation details. A simpler example of your difficulty will be clearer for the reader. Commented Dec 4, 2019 at 13:20

1 Answer 1

0

For the Integer part you have to do something like this:

System.out.println("Functions.substring(Functions.test(1,Functions.test(\"TestString1\")),0.9,99,\"TestString2\",true)".replaceAll("(?<=\\(|,)\\d+(?=\\)|,|$)", "Intger"));

For Decimal and String you have to adapt the code.

Probably you will be better served with Pattern and Matcher. An example here.

Sign up to request clarification or add additional context in comments.

Comments

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.