Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

c# - Regular Expression for validating numbers with one space and one optional special character

Can someone tell me how to validate sequence of numbers with one space and at the end there will be a optional special character say '#' and again followed by some 5 digits.

Example:

12345 6587#2584

I have tried with

(0(?:d{0,11}|(?=d* d*$)[d ]{0,12}))

but I don't know how to add optional '#' in the end followed by digits.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should do the trick

/^d+sd+(?:#d+)?$/

See it on rubular

^      beginning of string
d+    one or more numbers
s     any whitespace character
d+    one or more numbers
(?:    begin non-capturing group
  #    literal '#' character
  d+  one or more numbers
)      end non-capturing group
$      end of string

EDIT

/^0[ds]{,11}(?:#d{,5}?$/

Matches a string starting with 0, followed by a max of 11 numbers or spaces. Followed by an optional # with a max of 5 numbers after it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...