Regex expression for extracting numbers followed by 'KGS' -


i identify , extract numeric values followed 'kgs' (no space between number , 'kgs'. strings might contain other numbers not followed 'kgs' should ignored. example:

this 123 sample (500kgs) string. 

500 should extracted.

i suggest using less strict pattern here since not validating, extracting values. right now, regex extract 56, 578.56 or 52,456 values, , values 2,400.56 won't matched.

use

/\d[\d.,]*(?=\s*kgs)/i 

see regex demo. or, capturing:

/(\d[\d.,]*)\s*kgs/i 

the result in group 1. see another demo.

the \d[\d.,]* match digit, , 0 or more digits, . or ,.

the /i modifier make regex case-insensitive. depending on regex flavor, may use inline version of modifier, (?i) - put @ pattern start.


Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -