简体   繁体   English

正则表达式如何在字符串末尾匹配下划线?

[英]How do I Regex match an Underscore at end of string?

I have following pattern which fails with the underscore at the end but works otherwise 我有以下模式,该模式最后没有下划线,但可以正常工作

".\\d{1,2}\\.\\d{1,2}_." “。\\ d {1,2} \\。\\ d {1,2} _。”

EDIT: 编辑:

so I want all string that look with this: 所以我想要所有看起来像这样的字符串:

ANY TEXT HERE 10.20_ ANY TEXT HERE 这里的任何文本10.20_这里的任何文本

"Here is a sample 10.20_ that i use." “这是我使用的示例10.20_。”

"10.20_ is another sample." “ 10.20_是另一个示例。”

After the _ there is a . _有一个. which means one occurrence of a character except line break. 这表示除换行符外,一次出现一个字符。 So if the _ is at end it will not match the regex. 因此,如果_结尾,它将与正则表达式不匹配。 You have not given any examples of what you want, and just the regex, but you may want to make it .\\d{1,2}\\.\\d{1,2}_.? 您没有给出任何想要的示例,仅给出了正则表达式,但是您可能想使其成为.\\d{1,2}\\.\\d{1,2}_.?

尝试这个,

^.*\d{1,2}\.\d{1,2}_.*$

Your use of the period in the regex pattern is confusing. 您在正则表达式模式中使用句点令人困惑。 It says give me any character, do you want the white space before and possibly after? 它说给我任何字符,您要在空格之前还是之后? If so specify as \\s? 如果是这样,请指定为\\ s? for both the beginning or the end. 无论是开始还是结束。 If you just want the number simply use: 如果您只想要数字,请使用:

\d\d?\.\d\d?_

Or am I missing something? 还是我错过了什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM