How can I get the first and last part of one wordcombination using regex

Viewed 86

How can I get only the middle part of a combined name with PCRE regex?

name: 211103_TV_storyname_TYPE

result: storyname

I have used this single line: .(\d)+.(_TV_) to remove the first part: 211103_TV_

Another idea is to use (_TYPE)$ but the problem is that I don´t have in all variations of names a space to declare a second word to use the ^ for the first word and $ for the second.

The variation of the combined name is fix for _TYPE and the TV. The numbers are changing according to the date. And the storyname is variable. Any ideas?

Thanks

3 Answers

You could match as least as possible chars after _TV_ until you match _TYPE

\d_TV_\K.*?(?=_TYPE)
  • \d_TV_ Match a digit and _TV_
  • \K Forget what is matched until now
  • .*? Match as least as possible characters
  • (?=_TYPE) Assert _TYPE to the right

Regex demo

Another option without a non greedy quantifier, and leaving out the digit at the start:

_TV_\K[^_]*+(?>_(?!TYPE)[^_]*)*(?=_TYPE)
  • _TV_ Match literally
  • \K[^_]*+ Forget what is matched until now and optionally match any char except _
  • (?>_(?!TYPE)[^_]*)* Only allow matching _ when not directly followed by TYPE
  • (?=_TYPE) Assert _TYPE to the right

Regex demo


Edit

If you want to replace the 2 parts, you can use an alternation and replace with an empty string.

If it should be at the start and the end of the string, you can prepend ^ and append $ to the pattern.

\b\d{6}_TV_|_TYPE\b
  • \b\d{6}_TV_ A word boundary, match 6 digits and _TV_
  • | Or
  • _TYPE\b Match _TYPE followed by a word boundary

Regex demo

With your shown samples, please try following regex, this creates one capturing group which contains matched values in it.

.*?_TV_([^_]*)(?=_TYPE)

OR(adding a small variation of above solution with fourth bird's nice suggestion), following is without lazy match .*? unlike above:

_TV_([^_]*)(?=_TYPE)

Here is the Online demo for above regex

Explanation: Adding detailed explanation for above.

.*?_      ##Using Lazy match to match till 1st occurrence of _ here.
TV_       ##Matching TV_ here.
([^_]*)   ##Creating 1st capturing group which has everything before next occurrence of _ here.
(?=_TYPE) ##Making sure previous values are followed by _TYPE here.

Here i put some additional Screenshots to the post. With the Documentation that appears on the help button. And you see the forms and what i see.

Documentation

The regular expressions we use are based on PCRE - Perl Compatible Regular Expressions. Full specification can be found here: http://www.pcere.org and http://perldoc.perl.org/perlre.html

Summary of some useful terms:


Metacharacters


\ Quote the  next  metacharacter 
^ Match the  beginning  of  the  line 
. Match  any  character  (except  newline) 
$  Match  the  end  of  the  line  (or  before  newline  at  the  end) 
|  Alternation 
()  Grouping 
[]  Character  class 

Quantifiers


*  Match  0 or more times
+  Match  1 or more times
?  Match 1 or 0  times 
{n} Match exactly n times 
{n,}  Match  at  least  n  times 
{n,m}  Match  at  least  n  but  not  more  than  m  times

Charcter Classes


\w  Match  a  "word"  character  (alphanumeric  plus  mao} 
\W  Match  a  non-"word"  character 
\s  Match  a  whitespace  character 
\S  Match  a  non-whitespace  character 
\d  Match  a  digit  character 
\D  Match  a  non-digit  character 

Capture buffers


The bracketing construct (...) creates capture buffers. To refer to Within the same pattern, use \1 for the first, \2 for the second, and so on. Outside the match use "$" instead of "". The \ notation works in certain circumstances outside the match. See the warning below about \1 vs $1 for details. Referring back to another part of the match is called a backreference.


Examples


Replace story with certain prefix letters M N or E to have the prefix "AA":

`srcPattern  "(M|N|E  )  ([A-Za-z0-9\s]*)"`
`trgPattern  "AA$2"   `

`"N StoryWord1 StoryWord2" -> "AA StoryWord1 StoryWord2"`
`"E StoryWord1 StoryWord2" -> "AA StoryWord1 StoryWord2"`
`"M StoryWord1 StoryWord2" -> "AA StoryWord1 StoryWord2"`

"NoMatchWord StoryWord1 StoryWord2" -> "NoMatchWord StoryWord1 StoryWord2" (no match found, name remains the same)

Tool

Related