1

I have a string of tfa_2,tfa_3,tfa_4 and I have a regex of /^tfa_\d+/

How can I make it select all the tfa_1...tfa_999 and disregard the comma?

Thanks.

2
  • I am using a preg_match in php and inputing that regex. Commented Nov 4, 2015 at 2:05
  • If the input is guaranteed to contain only tfa_\d+ tokens, then just use preg_split to split at ,. If it is not, then also split, then filter the list according to the regex. Commented Nov 4, 2015 at 3:56

2 Answers 2

2

Assuming the input contains the exact format ^tfa_\d+(,tfa_\d+)*$ and no limit on the number after tfa_

The simplest solution is to use explode:

$tokens = explode(",", "tfa_2,tfa_3,tfa_4");

You can also use preg_split or preg_match, but they are overkill under this assumption:

$tokens = preg_split('/,/', "tfa_2,tfa_3,tfa_4");
$retcode = preg_match_all('/tfa_\d+/', "tfa_2,tfa_3,tfa_4", $tokens);

Assuming the input contains the exact format ^[^,]+(,[^,]+)*$, no quoted token, and no limit on the number after tfa_

You need to assert that tfa_\d+ is not preceded or suceeded by a non-separator (non-comma character) to prevent htfa_345, tfa_456xxx, -tfa_34 from being considered a match:

$retcode = preg_match_all('/(?<![^,])tfa_\d+(?![^,])/', "tfa_2,tfa_3,tfa_4,htfa_345,tfa_456xxx,-tfa_34", $tokens);

Alternate solution, by explode the string along , and use array_filter to filter out unwanted fields:

$fields = explode(",", "tfa_2,tfa_3,tfa_4,htfa_345,tfa_456xxx,-tfa_34");
$tokens = array_filter($fields, function ($v) {
    return preg_match('/^tfa_\d+$/', $v) == 1;
});

Assuming the input contains the exact format ^[^,]+(,[^,]+)*$, no quoted token, and the number after tfa_ can only be 1 to 999

Just modify the regex a bit to limit the number ([1-9]\d{0,2}):

$retcode = preg_match_all('/(?<![^,])tfa_[1-9]\d{0,2}(?![^,])/', "tfa_2,tfa_3,tfa_4456,htfa_345,tfa_456xxx,-tfa_34", $tokens);

Similar modification for explode + array_filter solution:

$fields = explode(",", "tfa_2,tfa_3,tfa_4456,htfa_345,tfa_456xxx,-tfa_34");
$tokens = array_filter($fields, function ($v) {
    return preg_match('/^tfa_[1-9]\d{0,2}$/', $v) == 1;
});
Sign up to request clarification or add additional context in comments.

Comments

1

From the update and the comments,

/(tfa_\d{1,3})+/g

In PHP:

$re = "/(tfa_\\d{1,3})+/"; 
$str = "tfa_1,tfa_002,tfa_999,tfa_xxx"; 

preg_match_all($re, $str, $matches);

Will match tfa_1, tfa_002, tfa_999 but not tfa_xxx or tfa_

1 Comment

This regex is too liberal. The OP already provides the format they want tfa_\d+.

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.