Regex: how to combine a word that does not end with a specific character

I would like to combine the whole word "word" - one that starts with a number character and can contain special characters, but does not end with "%".

Match these:

  • 112 (integers)
  • 10-12 (ranges)
  • 11/2 (fractions)
  • 11.2 (decimal numbers)
  • 1,200 (thousand separator)

but not

  • 12% (percent)
  • A38 (words starting with an alphabetic character)

I tried these regular expressions:

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

but returns '12% 'to '12%'

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

but returns' 12 'at '12%'

Can I make an exception for a term \Sthat ignores %? Or will you have to do something else?

I will use it in PHP, but just write as you would like, and I will convert it to PHP.

+5
7

:

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

:

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

\S*+ , , . "" % 12 12%.

, 1!abc, , \S, , .

+7

\S, %

, :

[^%\s]

. \b\d[^%\s]* Regexr

+1

KISS ():

/[0-9][0-9.,-/]*\s/
+1
\d+([-/\.,]\d+)?(?!%)

:

\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)
+1

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

Try this PCRE regular expression:

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

He should give you what you need.

0
source

I would suggest simply:

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

This does not apply exactly to decimal separators or ranges. (As an example). But possessive quantifiers ++will consume as many decimal places as they can. So you just need to check the next character with a simple statement. Worked for your examples.

0
source

All Articles