[英]c# regex pattern position and last 3 chars
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
. 例如
D2015987.txt
。 For me the important part of the regex is to extract D987
from the part of the file name. 对我而言,正则表达式的重要部分是从文件名部分中提取
D987
。
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. 我首先使用
Regex.Match(@"D\\d{4}|\\d{3}\\b+")
,当我得到值时,这似乎剪掉了最后一位数字。 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 ...
实际上,我正在尝试根据此值创建一个新字符串,因此我也可以使用
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. 我还需要帮助来创建一个新的值,该值将匹配类似于此
D11Q1987.txt ...
的模式D11Q1987.txt ...
从这个我也需要DQ987
部件。
Thanks in advance for your help. 在此先感谢您的帮助。 Dan
担
try this : pattern is ^(\\w)\\d+(\\d{3})\\.txt$
replace with $1$2
试试这个:模式是
^(\\w)\\d+(\\d{3})\\.txt$
替换为$1$2
now u use this code in your code according your (C#) syntax. 现在,您根据(C#)语法在您的代码中使用此代码。 i hope this work.
我希望这项工作。 c# code
C#代码
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# 输出C#
D2015987.txt
D987
another php solution 另一个PHP解决方案
$str = "D2015987.txt";
preg_match($re, $str, $matches);
$r=$matches[1].$matches[2];
var_dump($r );
output for php PHP的输出
string 'D987' (length=4)
(.)
grabs the first, capture group 1 (.)
抢到第一个,捕获组1 \\d+
matches any number of digits (but at least one) \\d+
匹配任意数量的数字(但至少一个数字) ([A-Za-z])
matches a single character, and captures it as group 1. ([A-Za-z])
匹配单个字符,并将其捕获为组1。 \\d
matches a single digit. \\d
匹配一个数字。 (\\d{3})
matches three digits. (\\d{3})
匹配三个数字。 \\.
escapes the period. txt
finishes it off looking for the literal characters txt txt
完成查找文字txt的操作 Regex: 正则表达式:
(.)\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 +
. 现在,如果最后一位数字是可变长度的,但始终以数字开头,我们只需将
{3}
更改为+
。
(.)\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. $1
包含D(以您的示例为例),`$ 2'包含Q(以您的示例为例),$ 3包含987,但这两个示例都取决于前面是否有一个我们可以用作标记但可以扔掉的数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.