• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

Regex extract only the closest numbers at both sides of a word (word boundaries?)

Hi,
I want to retrieve only numbers (can have a dot or coma)  that are just before or after one (see below) specific word ( "size" in this case).

123 size 23456 bbb    ->  123  and 23456
aa 123size23456             ->  123 and 23456
1.234 size 55.567       -> 1234 (or 1.234) and 55567
1234 size forget  345            -> 1234
aa12 size 14587          -> 14587

The ideal was to use the expression for several words  as    ?:size|area|length and also ignore characters not belonging to alphabet as space, /,-    ( example "1245/size/ 12"   would return   1245    12) if not too complex!
Can you help me?
0
novreisb
Asked:
novreisb
  • 4
  • 2
4 Solutions
 
aikimarkCommented:
This regex pattern seems to come close:
(\d[0-9.]*\d)?\s*size\s*(\d[0-9.]*\d)?

Open in new window

With your data, it parsed the following:
Match 0 Start(0) Length(14) 
	SubMatch 0: 123
	SubMatch 1: 23456

Match 1 Start(23) Length(12) 
	SubMatch 0: 123
	SubMatch 1: 23456

Match 2 Start(37) Length(17) 
	SubMatch 0: 1.234
	SubMatch 1: 55.567

Match 3 Start(56) Length(10) 
	SubMatch 0: 1234
	SubMatch 1: 

Match 4 Start(81) Length(13) 
	SubMatch 0: 12
	SubMatch 1: 14587

Open in new window

Question: In the last line, why aren't you getting the 12?
0
 
aikimarkCommented:
This pattern does not pick up the aa12 in the last line of your data.
\b(\d[0-9.]*\d)?\s*size\s*(\d[0-9.]*\d)?\b

Open in new window


please post some more examples of non-space characters used as delimiters.
0
 
aikimarkCommented:
When using this pattern:
\b(\d[0-9.]*\d)?\W*size\W*(\d[0-9.]*\d)?\b

Open in new window

the space delimited strings still parse correctly as well as this:
123 /size/ 23456 bbb
1245/size/ 12

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
novreisbAuthor Commented:
Hi aikimark,
Thank you very much by the solution! Just one very small aspect was not contemplated! The hypothesis of having more than one "keyword". But it is not relevant!
Rgrds
0
 
novreisbAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for novreisb's comment #a40239982

for the following reason:

The solution is not mine  and was my mistake , so I ask you to correct!
0
 
aikimarkCommented:
as far as different 'keywords' is concerned, you would just replace the "size" part of the pattern.  If you have a limited number of such keywords, they can be part of the regex pattern.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now