正则表达式,如何提取数据

stop__ 2011-07-14 03:57:17

用户输入 2"3' 或者 2 feet 3 inches
需要把2 ,3 提取出来
下面是我写的,发现无法匹配

string JoinedFootUnits = "foots|foot|feets|feet|ft|f|\"";
string JoinedInchUnits = "inches|inchs|inch|in|i|\'";

string pattern = @" \b
(?<footHeight>(\d+))
(\s*)
(?<footUnit>[" + JoinedFootUnits + @"]{1})
(\s*)
(?<inchHeight>\d+)*
(\s*)
(?<inchUnit>[" + JoinedInchUnits + @"])*
\b
";
Regex regex = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);

Match match = regex.Match(height);
...全文
79 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
stop__ 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyazhe 的回复:]

你的[]换成()来包含你的所有unit
[/Quote]
ok
stop__ 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyazhe 的回复:]

try
(?<footHeight>\d+)\s*(?<footUnit>"|feet)\s*(?<inchesHeight>\d+)\s*(?<inchesUnit>"|inches)
[/Quote]
多谢,可以。
是我搞错了。
stop__ 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyazhe 的回复:]

try
(?<footHeight>\d+)\s*(?<footUnit>"|feet)\s*(?<inchesHeight>\d+)\s*(?<inchesUnit>"|inches)
[/Quote]

不行,仅仅能提取 foot 不能提起 inches.
mymantou 2011-07-14
  • 打赏
  • 举报
回复
Regex re=new Regex("[a-z]");
re.Replace(string,"");
stop__ 2011-07-14
  • 打赏
  • 举报
回复
我试一试
兔子-顾问 2011-07-14
  • 打赏
  • 举报
回复
你的[]换成()来包含你的所有unit
兔子-顾问 2011-07-14
  • 打赏
  • 举报
回复
try
(?<footHeight>\d+)\s*(?<footUnit>"|feet)\s*(?<inchesHeight>\d+)\s*(?<inchesUnit>"|inches)

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧