Reputation: 33
I am trying to create a new string based on a file name. Part of the name contains irrelevant information like the current year. For example D2015987.txt
. For me the important part of the regex is to extract D987
from the part of the file name.
I started off by using Regex.Match(@"D\d{4}|\d{3}\b+")
, this seems to trim off the last digit when I get the values. In reality I am attempting to create a new string from this value, so I may also be able to use string newStr = Regex.Replace(pattern).value ...
I also need assistance with creating a new value that will match a pattern similar to this D11Q1987.txt ...
from this I need the DQ987
part as well.
Thanks in advance for your help. Dan
Upvotes: 1
Views: 467
Reputation: 3299
try this :
pattern is ^(\w)\d+(\d{3})\.txt$
replace with $1$2
now u use this code in your code according your (C#) syntax. i hope this work. c# code
using System.IO;
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// This is the input string we are replacing parts from.
string input = "D2015987.txt";
// Use Regex.Replace to replace the pattern in the input.
string output = Regex.Replace(input, @"^(\w)\d+(\d{3})\.txt$", "$1$2");
// Write the output.
Console.WriteLine(input);
Console.WriteLine(output);
}
}
output c#
D2015987.txt
D987
another php solution
$str = "D2015987.txt";
preg_match($re, $str, $matches);
$r=$matches[1].$matches[2];
var_dump($r );
output for php
string 'D987' (length=4)
Upvotes: 1
Reputation: 5510
(.)
grabs the first, capture group 1\d+
matches any number of digits (but at least one)([A-Za-z])
matches a single character, and captures it as group 1.\d
matches a single digit.(\d{3})
matches three digits.\.
escapes the period.txt
finishes it off looking for the literal characters txtRegex:
(.)\d+([A-Za-z])\d(\d{3})\.txt
Now, if the last digits are variable length, but always preceded by a digit, we simply change the {3}
to +
.
(.)\d+([A-Za-z])\d(\d+)\.txt
$1
contains D (by your example), `$2' contains Q (by your example) and $3 contains 987, but both examples depend on being preceded by a digit that we can use as a marker but throw away.
Upvotes: 0