0

I am trying to write the pattern matcher for the below string

show int sh 1/1/06
SHDSL 1/1/6 
Description                      3599979
Constellation (bits/baud)        30

I need to get the value of 'show int sh' and 'SHDSL' and 'Description' and so on...

It should shrink the white spaces and get the value of respective strings.

Can any one guide me to write the regex pattern for the same.?

1
  • 1
    writing a regex for such strings should be avoided. regex should be preferred where you can capture many similar patterns in your string. otherwise the regex will be more confusing and uglier just as in your case. I recommend you to parse your string line by line and prepare a properties table or map. Commented Oct 19, 2012 at 12:13

1 Answer 1

1

You can use this regex in multiline mode

^show int sh\s*(.*)$

^show int sh\s*checks for show int sh at the start ^ of the line before the required data

\s* matches 0 or more space till the first non space character

(.*)$ captures the required value till end of the line$ in group 1

So here are all the regex's

Use multiline mode

^show int sh\s*(.*)$

^SHDSL\s*(.*)$

^Description\s*(.*)$

^Constellation\s*(.*)$

OR a single regex

^((show int sh|SHDSL|Description|Constellation)\s*).*$

Sign up to request clarification or add additional context in comments.

5 Comments

Am using the following reqex as first two lines are not guarantee that we will get only show int sh so on. ".*\\s*(\\d+)\\s*.*\\s*(\\d+)\\x0D\\x0A" + //1 ,2 "\\sDescription\\s*[to]{0,2}\\s*(.)\\x0D\\x0A" + //3 "\\sConstellation \(bits/baud\)\\s*(.)\\x0D\\x0A" What's wrong in this????
Yes Anirudh I need in a single regex
@Anirudha: 1.) The OP appears to be using Jakarta ORO, which doesn't support lookbehind. 2.) Even in java.util.regex you can't use * or + in lookbehind expressions. 3.) You're trying to match the lookbehind at the beginning of the line, which means the text would have to appear before the beginning of the line. 4.) Lookbehind is the wrong tool anyway; just use (e.g.) ^SHDSL\s*(.*)$ and grab the desired part from group #1.
@AlanMoore is multiline supported
Yes, ORO supports multiline mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.