1

Given the string

170905-CBM-238.pdf

I'm trying to match 170905-CBM and .pdf so that I can replace/remove them and be left with 238.

I've searched and found pieces that work but can't put it all together.
This-> (.*-) will match the first section and
This-> (.[^/.]+$) will match the last section

But I can't figure out how to tie them together so that it matches everything before, including the second dash and everything after, including the period (or the extension) but does not match the numbers between.

help :) and thank you for your kind consideration.

melpomene
  • 84,125
  • 8
  • 85
  • 148
jlisham
  • 144
  • 1
  • 11
  • 1
    Try [`^.*-([0-9]+)\.[^.]+$`](https://regex101.com/r/5d3pfL/1). – Wiktor Stribiżew Aug 29 '17 at 21:18
  • that appears to match everything - I need to have it not match the number (and the extension may not always be pdf) --cheers – jlisham Aug 29 '17 at 21:21
  • 2
    If you just want to extract `238`, why match anything else? Just do `-(\d+)\.pdf$`. – melpomene Aug 29 '17 at 21:24
  • Possibility of `.PDF` vs `.pdf`? Any number of digits prior to the extension? What flavor of regex? – dawg Aug 29 '17 at 21:25
  • I'm using nintex to match and replace so need to match everything else so I can replace with "". Your solution returns the first section, having replaced everything/including after the last dash – jlisham Aug 29 '17 at 21:27
  • Can't you just do two regex replace actions in a row, one to strip the prefix and one to strip the suffix? – melpomene Aug 29 '17 at 21:37
  • yes - eventually I just added in two steps in the workflow and that will suffice – jlisham Aug 30 '17 at 13:29

2 Answers2

1

There are several options to achieve what you need in Nintex.

If you use Extract operation, use (?<=^.*-)\d+(?=\.[^.]*$) as Pattern.

See the regex demo.

Details

  • (?<=^.*-) - a positive lookbehind requiring, immediately to the left of the current location, the start of string (^), then any 0+ chars other than LF as many as possible up to the last occurrence of - and the subsequent subpatterns
  • \d+ - 1 or more digits
  • (?=\.[^.]*$) - a positive lookahead requiring, immediately to the right of the current location, the presence of a . and 0+ chars other than . up to the end of the string.

If you use Replace text operation, use

Pattern: ^.*-([0-9]+)\.[^.]+$
Replacement text: $1

See another regex demo (the Context tab shows the result of the replacement).

Details

  • ^ - a start of string anchor
  • .* - any 0+ chars other than LF up to the last occurrence of the subsequent subpatterns...
  • - - a hyphen
  • ([0-9]+) - Group 1: one or more ASCII digits
  • \. - a literal .
  • [^.]+ - 1 or more chars other than .
  • $ - end of string.

The replacement $1 references the value stored in Group 1.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

I don't know ninetex regex, but a sed type regex:

$ echo "170905-CBM-238.pdf" | sed -E 's/^.*-([0-9]*)\.[^.]*$/\1/'
238

Same works in Perl:

$ echo "170905-CBM-238.pdf" | perl -pe 's/^.*-([0-9]*)\.[^.]*$/$1/'
238
dawg
  • 98,345
  • 23
  • 131
  • 206