0

Sirs,

In Java Netbeans, I am going to take a String entered by a user, extract all of the capital letter chars, and then load the capital letters into a new char[] array. The string is going to consist entirely of capital letters and mathematical/algebraic operators (e.g. {}/+^*!() ...).

It is clear to me that there are a variety of expressions involving match and regular expressions -- this question has been addressed numerous times on this forum.

The problem that I am having is that every time a question similar to this has been asked, the solution was too specific to the OP's situation to be useful to me (e.g. the solution was only applicable to email addresses). Every time this question has been asked, the OP had considerable prior knowledge about what format the user input was going to come in.

In summary I am looking for a way to use regex and/or match, or anything else in Java Netbeans to pluck out all of the capital letters of an input string, irrespective of how scrambled up or mucked up the original string may be. For example if the user were to input:

a*j^A+=(B(({C}%A

The the resulting character string would be:

{"A", "B", "C", "A"}

Lastly, the tutorials I have come across at TutorialsPoint as well as the original documentation from Oracle are far too encyclopedic to be helpful. Searching for documentation on Regex produces enough reading material that it would take me days to dig through it all.

I'm more than capable of doing this in Python, just don't know how to do it in Java.

Regards,

Brent

2
  • Capital letters ! That's it ? Check this demo and let me know if this is what you are looking for ?
    – user2705585
    Commented Mar 13, 2016 at 19:51
  • Yes, the link that you provided does what I want to do. I'm not sure how to write code in java that will do it. Documentation on Regex in java is rather encyclopedic.
    – the_photon
    Commented Mar 13, 2016 at 19:57

1 Answer 1

0
    String t = "a*j^A+=(B(({C}%A";
    System.out.println(t.replaceAll("[\\Wa-z]", ""));

Returns string of capital letters only. Do what you want with them.

2
  • Thanks, I'm sure this will work. Not knowing that "replaceAll" was the magic command in java that did this trick, it was hard to find an answer.
    – the_photon
    Commented Mar 13, 2016 at 20:00
  • Your'e welcome. By the way, everything in java language and (perhaps) any other language is IDE independent. Java files would be the same in Eclipse, IntelliJ, etc.. But that doesn't applies to IDE specific files. Best way would be to use build tool such as maven or gradle. Then your project will be IDE independent completely.
    – callOfCode
    Commented Mar 13, 2016 at 20:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.