marcolopes
marcolopes

Reputation: 9287

REGEX to MATCH a given PREFIX and SUFFIX

I have strings composed of:

I need a REGEX to MATCH a given (variable) PREFIX and SUFFIX

Examples (prefix=ABCD, suffix=123456789):

String to match (several possible combinations):  
ABCD123456789  
abcd123456789  
ABCD 123456789  
abcd 123456789  
ABCD 123 456 789  
abcd 123 456 789  
123456789  
123 456 789  

String matches
+--------+-------------------+-------+  
| PREFIX |      SUFFIX       | MATCH |  
+--------+-------------------+-------+  
|        | 123456789         | YES   |  
|        | 123 456 789       | YES   |  
|        | 1 2 3 4 5 6 7 8 9 | YES   |  
| ABCD   | 123 456 789       | YES   |  
| ABCD   | 1 2 3 4 5 6 7 8 9 | YES   |  
+--------+-------------------+-------+  
|        | 12345678          | NO    |  
|        | 123 456 7890      | NO    |  
| ABCD   | 12345678          | NO    |  
| ABCD   | 123 456 7890      | NO    |  
|        | 123456789 ABCD    | NO    |  
| AB     | 123456789         | NO    |  
+--------+-------------------+-------+  

In other words:
- Prefix must MATCH if present (case insensitive) and must be IGNORED if not present
- Suffix must MATCH (spaces must be IGNORED!)

Equivalent JAVA code would be:

//remove prefix (case insensitive)
STRING.substring(STRING.toUpperCase().startsWith(PREFIX) ? PREFIX.length() : 0).
//remove spaces
replace(" ","").
//match
equals(SUFFIX);

Thanks a lot.

Upvotes: 4

Views: 9150

Answers (2)

Bohemian
Bohemian

Reputation: 425013

Remove all spaces in/around the numeric suffix first:

str.replaceAll(" (?=[\\d ]*$)", "").matches("(?i)(ABCD)?123456789")

Matches all your test cases (and doesn't match your non-matches).

Upvotes: 3

Miguel
Miguel

Reputation: 20633

I know your question has a Java tag but I'm not familiar with Java. Since Java and JavaScript have some similarities, here is a JavaScript implementation to get ideas from.

var rows = [{
    prefix: null,
    suffix: '123456789',
    assert: true
}, {
    prefix: null,
    suffix: '1123 456 789',
    assert: true
}, {
    prefix: null,
    suffix: '1 2 3 4 5 6 7 8 9',
    assert: true
}, {
    prefix: 'ABCD',
    suffix: '123 456 789',
    assert: true
}, {
    prefix: 'ABCD',
    suffix: '1 2 3 4 5 6 7 8 9',
    assert: true
},
{
    prefix: null,
    suffix: '12345678',
    assert: false
}, {
    prefix: null,
    suffix: '123 456 7890',
    assert: false
}, {
    prefix: 'ABCD',
    suffix: '12345678',
    assert: false
}, {
    prefix: 'ABCD',
    suffix: '123 456 7890',
    assert: false
}, {
    prefix: null,
    suffix: '123456789 ABCD',
    assert: false
}, {
    prefix: 'AB',
    suffix: '123456789',
    assert: false
}];

var PREFIX = 'ABCD';
var SUFFIX = '123456789';

var separator = '_';

var regex = new RegExp('^(' + PREFIX.toLowerCase() + ')?' + separator + '.*' + SUFFIX.replace(/\s+/g, '') + '$', 'g');

document.write('<pre>generated regex: ' + regex + '</pre>');

for (var i = 0; i < rows.length; i++) {
    regex.lastIndex = 0;
    var item = rows[i];
    var pre = (item.prefix || '').toLowerCase();
    var suf = item.suffix.replace(/\s+/g, '');
    
    var subject = pre + separator + suf;
    
    var result = regex.test(subject);
    
    document.write('<pre class="' + ((result === item.assert) ? 'success' : 'fail') + '">' + pre + ' ' + suf + ', expected: ' + item.assert + ', result: ' + result + '</pre>');

}
pre { padding: 5px; }
.success { border: 1px solid green; }
.fail { border: 1px solid red; }

Upvotes: 0

Related Questions