Reputation: 2416
It was terribly hard for me to make a fitting title for this question. It is much more easily explained by example:
test test test 2014 test test // 2014 (truthy)
2014testtest test2014testtest // [2014, 2014] (truthy)
test20141234testtest 2014test // => nothing (falsey)
test 1234 test 2014 test 2014 // => nothing (falsey)
So I want to know if the number 2014
is in the string, not accompanied by another other number except other instances of 2014
. Spaces need to be accounted for. As far as whether or not I get an array of matches back, I don't really care. This is truthy/falsey situation.
I'm working with PCRE syntax. Many thanks for the help.
Upvotes: 1
Views: 92
Reputation: 11041
You can use capturing-groups and backreferences:
^\D*+\b(\d+)\b\D*(\b\1\b\D*)*+$
Here digit segments are asserted on word boundaries and a backreference ensures the entire string replicates the same digit segments.
Here is a regex demo, however as the test cases are multiline [^\d\n]
is used instead. For your actual use case, this is not necessary.
Upvotes: 0
Reputation: 336238
If 2014
is the only sequence of valid digits that may occur anywhere in the string (but at least once), then the regex is rather simple:
^(?:\D*2014)+\D*$
Test it live on regex101.com.
Explanation:
^ # Start of the string
(?: # Start a non-capturing group that matches...
\D* # any number of characters except digits
2014 # followed by "2014".
)+ # Do this any number of times (but at least once).
\D* # After that, match any remaining non-digits...
$ # until the end of the string.
Upvotes: 4
Reputation: 1154
I don't think this can be done by regular expressions in one step. It sure can't be done by a finite automaton, but "regular expressions" as implemented by most languages have capabilities beyond that.
Try this:
substitute all consecutive non-digits by ';'
extract the first sequence of digits and substitute it in the entire string by a dummy ("a")
check if there are any digits left
Upvotes: 0