Phoenix
Phoenix

Reputation: 8923

Regex for 2 different strings accounting for optional elements

I have two strings "2007 AL PLAIN TEXT 5567 (NS)" and "5567" in the second string, I only want to extract one group out of both the strings which is 5567. How do I write a java regex for this ? The format will be 4 digit year, 2 digit jurisdiction, the string plain text, then the number I want to extract and finally (NS) but the problem is all except the number can be optional, How do I write a regex for this that can capture the number 5567 only in a group ?

Upvotes: 1

Views: 109

Answers (2)

Bohemian
Bohemian

Reputation: 425033

You can do it in one line:

String num = input.replaceAll("(.*?)?(\\b\\w{4,}\\b)(\\s*\\(NS\\))?$", "$2");

Assuming your target is "a word at least 4 alphanumeric characters long".

Upvotes: 1

A human being
A human being

Reputation: 1220

You need to use ? quantifier, which means that the match is optional, '?:' groups a match, but doesn't create a backreference for that group.Here is the code:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Regexp
{
    public static void main(String args[])
    {
    String x = "2007 AL PLAIN TEXT 5567 (NS)";
    String y = "5567";
    Pattern pattern = Pattern.compile( "(?:.*[^\\d])?(\\d{4,}){1}(?:.*)?");
    Matcher matcher = pattern.matcher(x);

    while (matcher.find()) 
        {
        System.out.format("Text found in x: => \"%s\"\n",
                  matcher.group(1));
        }

    matcher = pattern.matcher(y);

    while (matcher.find()) 
        {
        System.out.format("Text found in y: => \"%s\"\n",
                  matcher.group(1));
        }
    }
}

Output:

$ java Regexp
Text found in x: => "5567"
Text found in y: => "5567"

Upvotes: 0

Related Questions