English 中文(简体)
仅接受以百分比表示的投入
原标题:Need to accept only % symbol as an input string
  • 时间:2011-11-02 12:20:40
  •  标签:
  • java
  • regex

我只想接受一定百分比的符号(选择)作为数字的直观变量,就像投入的百分比一样。 E.G, 1000%或100%,或2000年可能,或显示投入为1000或2000年。

我们能否在定期表达的帮助下这样做? 或者可能还有其他一些验证。

请帮助

最佳回答

following

^[+-]?d+(.d+)?%?$

应同男性工作者合作

问题回答
^[0-9]+%?$

假设至少需要一名数字,只允许肯定数字(或<代码>0)。

如果你也想允许诽谤(你本可以提到这个问题的话),那么尝试

^[0-9]+(?:.[0-9]+)?%?$

这使得任何积极的分类或分类(但并非如<条码>.1或<条码>。 当然,接触性标志没有得到支持。

Absolutely. It sounds like you want to sanitize here as well, so "[0-9]{1,}%" or "[0-9]*.?[0-9]{1,}%" to ensure you match fractional percentages.

通常,我可能已经说过,但太少了(fun.org)。 EDIT

rel=“nofollow”>http://www.ual-expressions.info/vis.html

You could do it with a something besides a regex but that would just be a loop that asserts that each character of input is a decimal digit, period, or percent sign, and that they are in the appropriate order.

Hope this helps!

根据这一评论:

it seems to work perfect with patterns like 1000%, 100 but now problems with decimal values like 101.50 or 199.50%

^[-+]?d*.?d+%?$

<>Explanion>:

"
^        # Assert position at the beginning of the string
[-+]     # Match a single character present in the list “-+”
   ?        # Between zero and one times, as many times as possible, giving back as needed (greedy)
[0-9]    # Match a single character in the range between “0” and “9”
   *        # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
.       # Match the character “.” literally
   ?        # Between zero and one times, as many times as possible, giving back as needed (greedy)
[0-9]    # Match a single character in the range between “0” and “9”
   +        # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
       # Assert position at a word boundary
%        # Match the character “%” literally
   ?        # Between zero and one times, as many times as possible, giving back as needed (greedy)
$        # Assert position at the end of the string (or before the line break at the end of the string, if any)
"

As a side note : You should really have been able to account for the optional part from @ Tim s answer. Take a look at the tutorial proposed by the other answers.

You can try with the digits and % as an optional character.

^d+(.d+)?%?$




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签