我正在检查我的正则表达式是否与我的字符串匹配。
我有一个类似于somename_somthing.txt
的文件名,我希望将其与somename_*.txt
匹配,但是当我尝试传递应匹配的内容时,我的代码失败了。这是我的代码。
string pattern = "somename_*.txt";
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
using (ZipFile zipFile = ZipFile.Read(fullPath))
{
foreach (ZipEntry e in zipFile)
{
Match m = r.Match("somename_something.txt");
if (!m.Success)
{
throw new FileNotFoundException("A filename with format: " + pattern + " not found.");
}
}
}
答案 0 :(得分:0)
一般
正则表达式在此代码中匹配_与*表示零或更多下划线而不是您的意图。 *用于表示前一项的零个或多个。而是尝试
^somename_(.*)\.txt$
这与第一部分“somename_”完全匹配。
然后任何事情(。*)
最后结束“.txt”。反斜杠逃脱了'点'。
更具体
你也可以说,如果你只想在比赛的中间部分用字母而不是数字或符号:
^somename_[a-z]*\.txt$
答案 1 :(得分:0)
星号与下划线匹配并将其丢弃。
尝试:
public class Multimedium {
@Expose
private String url;
@Expose
private String format;
@Expose
private Integer height;
@Expose
private Integer width;
@Expose
private String type;
@Expose
private String subtype;
@Expose
private Object caption;
@Expose
private Object copyright;
/**
*
* @return
* The url
*/
public String getUrl() {
return url;
}
/**
*
* @param url
* The url
*/
public void setUrl(String url) {
this.url = url;
}
/**
*
* @return
* The format
*/
public String getFormat() {
return format;
}
/**
*
* @param format
* The format
*/
public void setFormat(String format) {
this.format = format;
}
/**
*
* @return
* The height
*/
public Integer getHeight() {
return height;
}
/**
*
* @param height
* The height
*/
public void setHeight(Integer height) {
this.height = height;
}
/**
*
* @return
* The width
*/
public Integer getWidth() {
return width;
}
/**
*
* @param width
* The width
*/
public void setWidth(Integer width) {
this.width = width;
}
/**
*
* @return
* The type
*/
public String getType() {
return type;
}
/**
*
* @param type
* The type
*/
public void setType(String type) {
this.type = type;
}
/**
*
* @return
* The subtype
*/
public String getSubtype() {
return subtype;
}
/**
*
* @param subtype
* The subtype
*/
public void setSubtype(String subtype) {
this.subtype = subtype;
}
/**
*
* @return
* The caption
*/
public Object getCaption() {
return caption;
}
/**
*
* @param caption
* The caption
*/
public void setCaption(Object caption) {
this.caption = caption;
}
/**
*
* @return
* The copyright
*/
public Object getCopyright() {
return copyright;
}
/**
*
* @param copyright
* The copyright
*/
public void setCopyright(Object copyright) {
this.copyright = copyright;
}
}
此处的(\ w +)将匹配此位置的组。
您可以在此处看到它匹配:https://regex101.com/r/qS8wA5/1
答案 2 :(得分:0)
正如所写,你的正则表达式
somename_*.txt
匹配(以不区分大小写的方式):
somename
,后跟_
),后跟txt
它将匹配源文本中的任何地方。你可能想写类似
的东西Regex myPattern = new Regex( @"
^ # anchor the match to start-of-text, followed by
somename # the literal 'somename', followed by
_ # a literal underscore character, followed by
.* # zero or of any character (except newline), followed by
\. # a literal period/fullstop, followed by
txt # the literal text 'txt'
$ # with the match anchored at end-of-text
" , RegexOptions.IgnoreCase|RegexOptions.IgnorePatternWhitespace
) ;
答案 3 :(得分:-1)
嗨,我认为模式应该是
string pattern = "somename_.*\\.txt";
此致