0

Currently I am checking a string for the following:

if(parseCommand.contains("vlan1") || parseCommand.contains("Fa0/1i")
  || parseCommand.contains("Fa0/1o") || parseCommand.contains("Fa1/0")                              
  || parseCommand.contains("Fa1/1") || parseCommand.contains("Fa1/2")
  || parseCommand.contains("Fa1/3") || parseCommand.contains("Fa1/4")
  || parseCommand.contains("Fa1/5") || parseCommand.contains("Fa1/6")                                    
  || parseCommand.contains("Fa1/7") || parseCommand.contains("Fa1/8") 
  || parseCommand.contains("Fa1/9") || parseCommand.contains("Fa1/11") 
  || parseCommand.contains("Gi0")) {
//do things here                              
}

However it may contain vlan1 up to vlan4094 and i have to check for these. What is the simplest way to do this?

I have tried this just to match a vlan 1-9 folowed by 0-3 digits but it doesn't find anything:

if(parseCommand.matches(".*vlan[1-9](\\d){0,3}") || parseCommand.contains("Fa0/1i")
  || parseCommand.contains("Fa0/1o")|| parseCommand.contains("Fa1/0")                               
  || parseCommand.contains("Fa1/1") || parseCommand.contains("Fa1/2")
  || parseCommand.contains("Fa1/3") || parseCommand.contains("Fa1/4")
  || parseCommand.contains("Fa1/5") || parseCommand.contains("Fa1/6")                                    
  || parseCommand.contains("Fa1/7") || parseCommand.contains("Fa1/8") 
  || parseCommand.contains("Fa1/9") || parseCommand.contains("Fa1/11") 
  || parseCommand.contains("Gi0")) {

Even if I try this nothing is found, why?

if(parseCommand.matches(".*vlan.*")

8
  • 1
    it should be enough vlan[\\d]{1, 4} I think. Can you give a sample command to test against? Commented Mar 5, 2013 at 10:59
  • What kind of command would you like? Any string that contains vlan1 - vlan4094, anything can come before or after Commented Mar 5, 2013 at 11:00
  • I could be missing something. But I'm not sure you should have those periods in the regex? Commented Mar 5, 2013 at 11:01
  • The regexp looks fine as "vlan1".matches(".*vlan[1-9](\\d){0,3}") returns true. Are you sure that parseCommand contains "vlan1" ? Commented Mar 5, 2013 at 11:02
  • I am sure because parseCommand.contains("vlan1") works fine. I tried both with and without @MementoMori, neither worked, will try again. Nothing was found with vlan[\\d]{1, 4} method, perhaps it needs .* Commented Mar 5, 2013 at 11:07

1 Answer 1

1

Use .matches("(?s).*vlan.*") or so for new line characters being catched by .; See DOTALL.

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

4 Comments

This works thanks, I'm not sire why the other answers were not working?
You then had a newline character \n in the string and that is not matched by .. The command (?s) also matches \n for dot.
I see, and would that newline have to come directly before or after vlan to be affecting it, or would a newline anywhere in the string matter?
Without (?s) either the first .* does not match (if newline before "vlan", or the second .* if newline after "vlan". By the way, \r is matched by (?s) DOTALL too.

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.