Skip to content Skip to sidebar Skip to footer

Regex A Date-like String

I am trying to extract a substring 022014-101 from a string str1: str1 = # I dont need the 2nd 022014, only the

Solution 1:

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

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

Explanation:

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

This will ensure your 6 digit date comes directly after a \ and include everything up until the next \.

Solution 2:

How about: (\d{6}.*?)\\, the first match group would give you what you want. See http://regex101.com/r/aP3bJ7

Solution 3:

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: Betweenoneand 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

Post a Comment for "Regex A Date-like String"