4

I need to strip off all the leading and trailing characters from a string upto the first and last digit respectively.

Example : OBC9187A-1%A
Should return : 9187A-1

How do I achieve this in Java?

I understand regex is the solution, but I am not good at it.
I tried this replaceAll("([^0-9.*0-9])","")
But it returns only digits and strips all the alpha/special characters.

3
  • 2
    Regex would be a good place to start... Commented Jul 9, 2015 at 19:44
  • 1
    Can you edit your question and post your attempts? Also explain problem you are having while creating solution. You simply need regex which will find digit one or more of any characters digit. This doesn't seem like very complex thing. Commented Jul 9, 2015 at 19:44
  • I edited my question to include what I have done. Commented Jul 9, 2015 at 20:07

2 Answers 2

3

Here is a self-contained example of using regex and java to solve your problem. I would suggest looking at a regex tutorial of some kind here is a nice one.

public static void main(String[] args) throws FileNotFoundException {
    String test = "OBC9187A-1%A";
    Pattern p = Pattern.compile("\\d.*\\d");
    Matcher m = p.matcher(test);

    while (m.find()) {
        System.out.println("Match: " + m.group());
    }
}

Output:

Match: 9187A-1

\d matches any digit .* matches anything 0 or more times \d matches any digit. The reason we use \\d is to escape the \ for Java since \ is a special character...So this regex will match a digit followed by anything followed by another digit. This is greedy so it will take the longest/largest/greediest match so it will get the first and last digit and anything in between. The while loop is there because if there was more than 1 match it would loop through all matches. In this case there can only be 1 match so you can leave the while loop or change to if like this:

if(m.find()) 
{
    System.out.println("Match: " + m.group());
}
Sign up to request clarification or add additional context in comments.

3 Comments

I used a similar expression with replaceAll, but it did not work, returned only digits.
@user2166045 what expression did you use?
@user2166045 in this case you could use replaceAll there are 2 examples below but since you are trying to pull out a match from the String I would use the Pattern and Matcher to match a portion of your String. This allows you to keep the original String if you want or you can replace it myString = m.group(); with the matched String.
3

This will strip leading and trailing non-digit characters from string s.

String s = "OBC9187A-1%A";
s = s.replaceAll("^\\D+", "").replaceAll("\\D+$", "");
System.out.println(s);
// prints 9187A-1

DEMO

Regex explanation
^\D+

^ assert position at start of the string
\D+ match any character that's not a digit [^0-9]
     Quantifier: + Between one and unlimited times, as many times as possible

\D+$

\D+ match any character that's not a digit [^0-9]
     Quantifier: + Between one and unlimited times, as many times as possible
  $ assert position at end of the string

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.