• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

Regex

how do you specify a pattern for

* space * & large

meaning any number of other characters before and after "space", followed by "large"
0
bhomass
Asked:
bhomass
  • 9
  • 6
  • 3
2 Solutions
 
for_yanCommented:
        Pattern p = Pattern.compile(".*space.*large");

        Matcher m1 = p.matcher("sdfsdfspace  large");

        if(m1.find())System.out.println("matched");

Open in new window


matched

Open in new window

0
 
Terry WoodsIT GuruCommented:
Note for_yan's pattern still requires the 2 words to be on the same line. Did you want to capture or replace part of the string at all, or just detect that it matched
0
 
Terry WoodsIT GuruCommented:
And are you working with Java, or Perl, or something else?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
for_yanCommented:

This is matcvhing even if there is end of line:
                             String sep = System.getProperty("line.separator");

     //  Pattern p = Pattern.compile(".*space.*large");
          Pattern p = Pattern.compile(".*space.*large", Pattern.DOTALL);

        Matcher m1 = p.matcher("sdf" +sep +  " sdfspace " + sep + "  large");

        if(m1.find())System.out.println("matched");

Open in new window


matched

Open in new window

0
 
for_yanCommented:



Another way to specify that end of line should also be acceptable.
the code below works (when we use ("(?s:.*space.*large)") instead of ("(.*space.*large)") )
 ?s: is a "single-line mode" and this should work not only in Java - as opposed to Pattern.DOTALL which is obviously
Java specific
                             String sep = System.getProperty("line.separator");

      Pattern p = Pattern.compile("(?s:.*space.*large)");
        // p = Pattern.compile("(.*space.*large)");

         // Pattern p = Pattern.compile(".*space.*large", Pattern.DOTALL);

        Matcher m1 = p.matcher("sdf" +sep +  " sdfspace " + sep + "  large");

        if(m1.find())System.out.println("matched");

Open in new window


 matched 

Open in new window

0
 
bhomassAuthor Commented:
I follow for_yan almost all the way. please explain to me what is ?s:

I actually need this to be generally applicable, language independent, but I will test it out using java whenever possible.
0
 
for_yanCommented:

The same thing but more informative output:

                             String sep = System.getProperty("line.separator");

      Pattern p = Pattern.compile("(?s:.*space.*large)");
        // p = Pattern.compile("(.*space.*large)");

         // Pattern p = Pattern.compile(".*space.*large", Pattern.DOTALL);

        Matcher m1 = p.matcher("sdf" +sep +  " sdfspace " + sep + "  large");

        if(m1.find())System.out.println(("sdf" +sep +  " sdfspace " + sep + "  large").substring(m1.start(),m1.end()));

Open in new window


Output:
sdf
 sdfspace 
  large

Open in new window

0
 
for_yanCommented:
(?s:here your regex) - this ?s: means single-line mode which means that dot also matches new line charcter
0
 
for_yanCommented:
but you should be careful - if those are many lines in the file - I guess in this single-line mode
it will match all lines before

0
 
bhomassAuthor Commented:
is there a way to specify the distance between "space" and large is less than 30 char?
0
 
Terry WoodsIT GuruCommented:
I haven't seen this syntax for the single-line pattern modifier before:
      Pattern p = Pattern.compile("(?s:.*space.*large)");
Does it just apply to the group it's in?

I'm more familiar with this:
      Pattern p = Pattern.compile("(?s).*space.*large");

And I see for_yan has just explained it, so I won't write any more!
0
 
for_yanCommented:

Actually, I think this regex should be language indpendent - I don't think there is anything java specific here:
"(?s:.*space.*large)"
0
 
Terry WoodsIT GuruCommented:
is there a way to specify the distance between "space" and large is less than 30 char?

Yes:
      Pattern p = Pattern.compile("(?s).*space.{0,29}large");
0
 
for_yanCommented:


 Pattern p = Pattern.compile("(?s:.*space.{1,30}large)");
0
 
Terry WoodsIT GuruCommented:
And actually, the leading .* is redundant, so this would be fine too:

      Pattern p = Pattern.compile("(?s)space.{0,29}large");
0
 
for_yanCommented:
Yes 0,29 iif you ewant strict and are ok witth no space
0
 
Terry WoodsIT GuruCommented:
specify the distance between "space" and large is less than 30 char?
      Pattern p = Pattern.compile("(?s)space.{0,29}large");  // also allows no gap between the words
      Pattern p = Pattern.compile("(?s)space.{1,29}large");

specify the distance between "space" and large is less than or equal to 30 char?
      Pattern p = Pattern.compile("(?s)space.{0,30}large");  // also allows no gap between the words
      Pattern p = Pattern.compile("(?s)space.{1,30}large");
0
 
bhomassAuthor Commented:
great answers
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now