bonna
bonna

Reputation: 1615

Regex: how to match an word that doesn't end with a specific character

I would like to match the whole "word"—one that starts with a number character and that may include special characters but does not end with a '%'.

Match these:

but not

I've tried these regular expressions:

(\b\p{N}\S)*)

but that returns '12%' in '12%'

(\b\p{N}(?:(?!%)\S)*)

but that returns '12' in '12%'

Can I make an exception to the \S term that disregards %? Or will have to do something else?

I'll be using it in PHP, but just write as you would like and I'll convert it to PHP.

Upvotes: 7

Views: 3419

Answers (7)

CAFxX
CAFxX

Reputation: 30291

KISS (restrictive):

/[0-9][0-9.,-/]*\s/

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

This matches your specification:

\b\p{N}\S*+(?<!%)

Explanation:

\b       # Start of number
\p{N}    # One Digit
\S*+     # Any number of non-space characters, match possessively
(?<!%)   # Last character must not be a %

The possessive quantifier \S*+ makes sure that the regex engine will not backtrack into a string of non-space characters it has already matched. Therefore, it will not "give back" a % to match 12 within 12%.

Of course, that will also match 1!abc, so you might want to be more specific than \S which matches anything that's not a whitespace character.

Upvotes: 8

etuardu
etuardu

Reputation: 5516

\d+([-/\.,]\d+)?(?!%)

Explanation:

\d+        one or more digits
(
   [-/\.,]     one "-", "/", "." or ","
   \d+         one or more digits
)?         the group above zero or one times
(?!%)      not followed by a "%" (negative lookahead)

Upvotes: 1

stema
stema

Reputation: 92976

Can i make an exception to the \S term that disregards %

Yes you can:

[^%\s]

See this expression \b\d[^%\s]* here on Regexr

Upvotes: 1

mario
mario

Reputation: 145482

I would suggest just:

(\b[\p{N},.-]++(?!%))

That's not very exact regarding decimal delimiters or ranges. (As example). But the ++ possessive quantifier will eat up as many decimals as it can. So that you really just need to check the following character with a simple assertion. Did work for your examples.

Upvotes: 0

hrwath
hrwath

Reputation: 125

Try this PCRE regex:

/^(\d[^%]+)$/

It should give you what you need.

Upvotes: 0

ahmetunal
ahmetunal

Reputation: 3950

try this one

preg_match("/^[0-9].*[^%]$/", $string);

Upvotes: 0

Related Questions