use regular expression to prase specified protocol

jackson

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.

Kenster

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

use regular expression to prase specified protocol

분류에서Dev

Javascript regular expression to add protocol to url string

분류에서Dev

Regular Expression for matching number greater than specified decimal number

분류에서Dev

how to use regular expression replacement in PowerShell

분류에서Dev

How to use regular expression edit the content?

분류에서Dev

Use regular expression in assert_file

분류에서Dev

how to use the below regular expression in java code

분류에서Dev

Regular expression for && and &

분류에서Dev

Use an regular expression to grab part of a string in js/jquery

분류에서Dev

How to use Regular Expression for whole numbers with C#

분류에서Dev

Can I use a Regular Expression within jQuery.inArray()

분류에서Dev

Can I use a regular expression to convert these examples to markdown?

분류에서Dev

How to use a regular expression to check for duplicate characters in a string?

분류에서Dev

Regular expression not working with \ and ]

분류에서Dev

Regular expression filename

분류에서Dev

Regular Expression Matching - Tracks

분류에서Dev

Javascript, regular expression and link

분류에서Dev

Python Regular Expression not match

분류에서Dev

Regular expression formation

분류에서Dev

Surname regular expression

분류에서Dev

Matlab Regular expression query

분류에서Dev

django / python - regular expression

분류에서Dev

Bash - find regular expression

분류에서Dev

Regular expression in GWT

분류에서Dev

Regular Expression for group

분류에서Dev

Java regular expression usage

분류에서Dev

Balanced regular expression

분류에서Dev

Regular expression OR in python findall

분류에서Dev

ReplaceAll and Regular Expression