With lookbehind support
lookbehind -; , .
- Javascript:
- Python:
- Java:
- .NET:
Python
Python, lookbehind, , \d{1,2}, , . "" , , . - :
(?<=^\d\/)\d{1,2}|(?<=^\d\d\/)\d{1,2}
, , lookbehind :
(?:(?<=^\d\/)|(?<=^\d\d\/))\d{1,2}
( , \d ).
, , :
^\d{1,2}\/(\d{1,2})
, findall , 1 , . , lookbehind, (, ).
:
p = re.compile(r'(?:(?<=^\d\/)|(?<=^\d\d\/))\d{1,2}')
print(p.findall("12/34/56"))
print(p.findall("1/23/45"))
p = re.compile(r'^\d{1,2}\/(\d{1,2})')
print(p.findall("12/34/56"))
print(p.findall("1/23/45"))
p = re.compile(r'(?<=^\d{1,2}\/)\d{1,2}')
Java
Java lookbehind, \d{1,2} . :
String text =
"12/34/56 date\n" +
"1/23/45 another date\n";
Pattern p = Pattern.compile("(?m)(?<=^\\d{1,2}/)\\d{1,2}");
Matcher m = p.matcher(text);
while (m.find()) {
System.out.println(m.group());
}
, (?m) Pattern.MULTILINE, ^ . , \ escape- , "\\", Java.
C-Sharp
# lookbehind. , + lookbehind:
var text = @"
1/23/45
12/34/56
123/45/67
1234/56/78
";
Regex r = new Regex(@"(?m)(?<=^\d+/)\d{1,2}");
foreach (Match m in r.Matches(text)) {
Console.WriteLine(m);
} // "23", "34", "45", "56"
, Java, # @- , \.
#:
Regex r = new Regex(@"(?m)^\d+/(\d{1,2})");
foreach (Match m in r.Matches(text)) {
Console.WriteLine("Matched [" + m + "]; month = " + m.Groups[1]);
}
text, :
Matched [1/23]; month = 23
Matched [12/34]; month = 34
Matched [123/45]; month = 45
Matched [1234/56]; month = 56