Looking for regex to match before and after a number

975 views Asked by At

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.

2

There are 2 answers

2
Wiktor Stribiżew On BEST ANSWER

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.

3
dawg On

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