0

If I have the string

thisIsSomthing=4891\r\n
thisIsSomthingElse=27398472\r\n
thisIsNumber1=1\r\n

how would I find

thisIsNumber1

and then return 1 using regex

2
  • 1
    Is this string in a file? Commented Feb 28, 2013 at 1:13
  • Why would you use a regex for that? Also, I think we need a better description of what you want. The way the question is asked, your needs could easily be interpretted in many different ways. Commented Feb 28, 2013 at 1:24

3 Answers 3

3

This assumes you really have that posted content in a string, and not in a file. As you're dealing with properties, you should use Properties and not a regex:

    String yourString = ...
    Properties prop = new Properties();
    try {
        prop.load(new StringReader(yourString));
        String result = prop.getProperty("thisIsNumber1");
        System.out.println(result);
    } catch (IOException e) {
        System.out.println("Error loading properties:");
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is the best answer.
1

/thisIsNumber1=(\d+).*/

It'll be in capture group 1.

Comments

0
 String line="thisIsNumber1=1\r\n";
 String temp=line.split("\\r?\\n")[0].split("=")[1];
 System.out.println("Value="+temp+"*"); // 1* "*" shows nothing is concatenated after 
 the character in the output

1 Comment

If you want that line in a single string it would be String line = "thisIsNumber1=1\r\n";

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.