Reputation: 18097
consider the following string:
"abc123 123 123abc abc123abc"
Now i want to select 123 that is not by itself. So all digit but digit \s\d+\s+
I've tried ton of stuff but no..
Upvotes: 2
Views: 517
Reputation: 9131
So here is a short solution to your problem:
\B123|123\B
\B
matches non word boundaries. So before or after 123
a part of a word must be found.
This will match all 123 without the one that is standing alone.
Edit 1:
If 123
stands for a sequence of digits and abc
for a sequence of letters you could try
[a-zA-Z]\d+|\d+[a-zA-Z]
Unfortunately for this solution you have to create matching groups to retrieve the digits. This is the regexp then
[a-zA-Z](\d+)|(\d+)[a-zA-Z]
Upvotes: 4
Reputation: 2128
My suggestion in your case would be:
[^\s\d]+\d+[^\s\d]*|[^\s\d]*\d+[^\s\d]+
Detailed explanation:
[^\s\d]+ // Represents one or more non-whitespace and non-digit character
\d+ // Represents one or more digit (your required sequence)
[^\s\d]* // Represents zero or more non-whitespace and non-digit characters
| // Represents logical OR operation
[^\s\d]* // Zero or more non-WS and non-digit
\d+ // Your sequence
[^\s\d]+ // One or more non-WS and non-digit
NOTE: [^\s\d]*
entries are used for capturing the whole group in case of abc123abc
.
UPD: In the current version, from the string abc13 124233 356abc abc12333abc
my regex would match abc13
, 356abc
and abc12333abc
.
Also tested with Rubular.
Upvotes: 1
Reputation: 1953
Probably you're looking for lookahead assertion and lookbehind assertion in regular expression.
[^\s]*(?<!\s)123(?!\s)[^\s]*
will match abc123abc only
But the bad news is that javascript regular expression doesn't support lookahead/lookbehind assertion
Upvotes: 0
Reputation: 46836
If what you're looking for is the string of digits as a distinct word, regexp can use word boundaries. For example:
$ echo "abc123 123 123abc abc123abc" | egrep -o '\<[[:digit:]]+\>'
123
Or perhaps a better example:
$ echo "abc123 234 345abc abc456abc" | egrep -o '\<[[:digit:]]+\>'
234
Now, I'm not a JavaScript guy. But if as @wumpz seems to suggest, JavaScript's regex parser uses \B
in place of \<
and \>
then a regex of \B[[:digit:]]+\B
would seem to do the trick, assuming JavaScript understands classes.
Upvotes: 0
Reputation: 5198
The following regex will work in the specific case:
/\w+123\w+|\w+123|123\w+/
As in:
"abc123 123 123abc abc123abc".match(/\w+123\w+|\w+123|123\w+/g);
Will output:
["abc123", "123abc", "abc123abc"]
Assuming that abc
and 123
are both obfuscations then you'll need to alter 123 in the regex to target your relevant case.
Upvotes: 3