My server receive a message like #1037szdx001A8911*
,now I need to get szdx001
called sid
The length of sid
is decided by the character 7
. The length will be 8 or 9 . It is easy to use String.substring()
to get the length first. Then I will get the sid
according to the length.
But Now I try to use regular expression.Here is my pattern. #(\\d)(\\d{2})(\\d)(\\w{7})(\\w)(\\d{4})*
My Problem is that how I can replace 7 with the following group in java grammar.
Judging from the regular expression that you provided, it looks like the input string normally splits up into these fields:
# 1 03 7 szdx001 A 8911 *
and the 5th field (szdx...) is the only field that is variable in size? If that's correct, then you don't need to know its size to parse the string into fields:
#(\d)(\d{2})(\d)(\w+)(\w)(\d{4})*
The above regular expression will parse the first four fields and the last fields as fixed-size. The variable-sized field will get whatever text lies between the 4th and 6th fields. You would have to check separately that the fifth field is the correct length. [Note I've removed the doubled backslashes here; we're just talking about the regular expression, not the correct way to specify backslashes in a string constant.]
A slightly more restrictive form of the above is:
#(\d)(\d{2})(\d)(\w{7,9})(\w)(\d{4})*
This limits the fifth field to between 7 and 9 characters.
If you must parse the string using only a regular expression, and you can't validate the length of the fifth field separately, then you could do something like this:
#(\d)(\d{2})(?:7(\w{7})|8(\w{8})|9(\w{9}))(\w)(\d{4})*
The important part is (?:7(\w{7})|8(\w{8})|9(\w{9}))
. This is a non-capturing group containing three alternatives: '7' followed by a 7-character string, or '8' followed by an 8-character string, or '9' followed by a 9-character string. Note that the numbers of each capture group will change here; the 7-, 8-, and 9-character strings are each in different capture groups, so your code will have to examine all three.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다