Regex - date type string

I am trying to extract a substring 022014-101from a string str1:

str1 = <C:\User\Test\xyz\022014-101\more\stuff\022014\1>
             # I dont need the 2nd 022014, only the first occuring one 022014-101

I usually use split("\\")and split the line after \to get the [-5]'th element , but this is bad if I have more subfolders ... As you can see the first 6 digits 022014-101represent the date plus some characters (-101 in this case). I think I should use regex, but how can I match 6 digits and get everything until it happens \. I don't have much experience with regex, does anyone know a solution? Thanks.

+4
source share
3 answers

Try the following: (?<=\\)[\d]{6}[^\\]*

: http://regex101.com/r/qQ0tR3

:

(?<=\\)         # Lookbehind for a \ (escaped \\)
[\d]{6}         # Followed by 6 digits
[^\\]*          # Followed by 0+ characters other than a \ (escaped \\)

, 6- \ \.

+3

: (\d{6}.*?)\\, , . . http://regex101.com/r/aP3bJ7

+1

Try this (the first match will always be what you need):

\\([\d\-]+)\\

Demo:

http://regex101.com/r/pI0yP7

Explanation:

"\\([\d-]+)\\"

\\ matches the character \ literally
        1st Capturing group ([\d-]+)
        [\d-]+ match a single character present in the list below
        Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
        \d match a digit [0-9]
        - the literal character -
\\ matches the character \ literally
0
source

Source: https://habr.com/ru/post/1527947/


All Articles