开发者

Help me generate this regex

I have been trying to use regex but I cant seem to get it to work. I'm trying to use regex with the asp.net RegularExpressionValidator. What I want it to do is to basically disallow leading and trailing spaces only.

So,

"hello "                 // would not work.
"hello my name is"       // would work.

But in all my attempts it says bad input no matter what I put in.

Here is what I used:

^\s[a-z]+\s$

Can someone please provide one that works?

And also, what does it mean when someone says the regex returned a match?

Edit:

I have tried all the solutions in the thread and none of them work for me. Maybe it is something else wrong and not the regex? Heres is the relevant code:

                            <InsertItemTemplate>
                            <asp:TextBox CssClass="tbUpdateSummary" TextMode="MultiLine" Text='<%# Bind("updateSummary") %>'
                                ID="tbUpdateSummaryInsert" runat="server"></asp:TextBox>
                            <asp:Req开发者_如何学PythonuiredFieldValidator ID="valUpdateSummaryInsert1" runat="server" Display="None"
                                ControlToValidate="tbUpdateSummaryInsert" ErrorMessage="Update summary must not be empty.">
                            </asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator ValidationExpression="^\s+|\s$" ID="valUpdateSummaryEdit2"
                                runat="server" Display="None" ControlToValidate="tbUpdateSummaryInsert"  ErrorMessage="Update summary must not contain leading or trailing spaces."> </asp:RegularExpressionValidator>
                            <%--Put c# code to validate length (max 200)--%>
                            <asp:CustomValidator></asp:CustomValidator>
                        </InsertItemTemplate>

^[^\s].+[^\s]$ <======= This one actually works!!


Given that the RegularExpressionValidator is looking for matches against good input rather than vice-versa:

The following regex will match anything that is not preceeded or followed by spaces:

^(?!\s+).+(?<!\s+)$

although the Trim option mentioned in the comments to the question seems like a more sensible way to proceed.


Try this regex it should match everything that don't have a white space at the start and end

^[^\s].+[^\s]$


The regex validator is a positive test. You have three cases to test for

  • input of length zero.
  • input of length 1
  • input of length > 1

Since you have a Required validator on the input control, the first case won't occur. The following vanilla regular expression should work:

^[^\s]([.\n]*[^\s])?$

You may read it as saying that valid input consists of:

  • Start-of-line, followed by,
  • Exactly 1 non-whitespace character, followed by
  • An optional subexpression, consisting of
    • zero or more of any character, including LF (by default the metacharacter . matches any character except LF), followed by
    • exactly 1 nonwhitespace character, Followed by,
  • end-of-line

Since you have a multi-line edit control in your example, use of .* in the middle instead of [.\n]* I use will result in the match terminating at the first LF character found. See http://msdn.microsoft.com/en-us/library/az24scfc.aspx#character_classes for details of .Net regular expression character classes.

[Edited to Note]

Testing this shows that for some reason, .Net doesn't like [] character classes containing . (or it treats it as a literal period character). This regular expression works:

^([^\s])((.|\n)*[^\s])$

But it allows for trailing line breaks.

However, this regular expression is better (for some definition of better). It (A) doesn't involve backtracking, and (B) won't allow trailing line breaks:

^[^\s]+(\s+[^\s]+)*$
  • Start-of-line, followed by
  • 1 or more non-whitespace characters, followed by
  • zero or more repetitions of
    • 1 or more whitespace characters, followed by
    • 1 or more non-whitespace characters, Followed by,
  • End-of-Line.

Good Luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜