1

I am trying to match, the following cases:

1. Get either between or if only one x exists the end

Example:

| Matches/Cases     | Result |
|-------------------|--------|
| 200 x 90 x 14     | 90     |
| 90x200            | 200    |
| 200 x 90x20       | 90     |
| 60,4 x46,5 x 42,6 | 46,5   |
| 90x190,9          | 190,9  |

2. Get if two x exist the final one, and if only one exist no result

Examples:

| Matches/Cases     | Result |
|-------------------|--------|
| 200 x 90 x 14     | 14     |
| 90x200            | -      |
| 200 x 90x20       | 20     |
| 60,4 x46,5 x 42,6 | 42,6   |
| 90x190,9          | -      |

I stuck at getting one specific case! I tried to match with the following regex x\s?((\d+(?:,\d+)?))\s?, but I still get only the last part of the cases like for 90x200 I get 200, but for 200 x 90 x 14 I get 90 x 14.

Any suggestions of two regex that works for case 1 or case 2?

I appreciate your replies!

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • These are two requirements, right? Why should the same regex match **90** in `200 x 90 x 14` and **14** in the very same string? Additionally, which language are you using? – Jan Sep 17 '16 at 18:40
  • For the first: [**`[\d,]+\h*x\h*([\d,]+)(?:\h*x*[\d,]+)?`**](https://regex101.com/r/uC3iN7/1) – Jan Sep 17 '16 at 18:41
  • And for the second: [**`[\d,]+\h*x\h*[\d,]+\h*x\h*([\d,]+)`**](https://regex101.com/r/aW2lN7/1) – Jan Sep 17 '16 at 18:43

2 Answers2

1

Just turning the comments into an answer.
For the first case, you could use:

[\d,]+\h*x\h*([\d,]+)(?:\h*x*[\d,]+)?

See a demo on regex101.com.


And the second:
[\d,]+\h*x\h*[\d,]+\h*x\h*([\d,]+)

Another demo on regex101.com.


Hint: Replace \h with either [ ]* or \s* if it is not supported.
Jan
  • 42,290
  • 8
  • 54
  • 79
1

I tried to match with the following regex x\s?((\d+(?:,\d+)?))\s?, but I still get only the last part of the cases.

Actually by your own RegEx you are going to capture all digits or floats followed by a x. So it's not only last part but all similar occurrences.

Solution (main regex):

(?: *(\d+(?:,\d+)?) *(?:x|$))

If you want it for case #1 then append quantifier {2}

(?: *(\d+(?:,\d+)?) *(?:x|$)){2}

Live demo

If you want it for case #2 append quantifier {3}

(?: *(\d+(?:,\d+)?) *(?:x|$)){3}

Live demo

m modifier should be set in both cases

revo
  • 47,783
  • 14
  • 74
  • 117
  • Thank you very much for your answer! I am using excel as regular expression engine - http://analystcave.com/excel-regex-tutorial/. Hence, the match does not work, as excel can only match and not get groups. Any suggestions how to exactly match the cases? – Carol.Kar Sep 17 '16 at 20:55
  • The link you provided states it supports capturing groups. So there should be a functionality to work with captured groups as well. @mrquad – revo Sep 17 '16 at 21:00
  • My problem is that I get the wrong string back, like f.ex. `200 x 90 x 14`, I get back `200 x 90 x` instead of `90`. Any suggestions why? – Carol.Kar Sep 17 '16 at 21:06
  • Could you update your question with your code snippet? – revo Sep 17 '16 at 21:08
  • I am using the following regex function for excel: http://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in-microsoft-excel-both-in-cell-and-loops – Carol.Kar Sep 17 '16 at 21:15
  • `inputMatches(0).subMatches(0)` should return **first** capturing group of matched string (which you need it). If not try to play with indexes. Sorry that I'm not a VBScript guy. – revo Sep 17 '16 at 21:25