Solved

Regex

Posted on 2011-09-18
18
328 Views
Last Modified: 2012-05-12
how do you specify a pattern for

* space * & large

meaning any number of other characters before and after "space", followed by "large"
0
Comment
Question by:bhomass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 3
18 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36557627
        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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36557641
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
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36557644
And are you working with Java, or Perl, or something else?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36557671

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
 
LVL 47

Expert Comment

by:for_yan
ID: 36557687



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
 

Author Comment

by:bhomass
ID: 36557708
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36557711

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
 
LVL 47

Expert Comment

by:for_yan
ID: 36557716
(?s:here your regex) - this ?s: means single-line mode which means that dot also matches new line charcter
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36557717
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
 

Author Comment

by:bhomass
ID: 36557718
is there a way to specify the distance between "space" and large is less than 30 char?
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 100 total points
ID: 36557720
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36557723

Actually, I think this regex should be language indpendent - I don't think there is anything java specific here:
"(?s:.*space.*large)"
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36557726
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
 
LVL 47

Accepted Solution

by:
for_yan earned 140 total points
ID: 36557727


 Pattern p = Pattern.compile("(?s:.*space.{1,30}large)");
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36557728
And actually, the leading .* is redundant, so this would be fine too:

      Pattern p = Pattern.compile("(?s)space.{0,29}large");
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36557730
Yes 0,29 iif you ewant strict and are ok witth no space
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36557733
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
 

Author Comment

by:bhomass
ID: 36557740
great answers
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java imports not found 4 52
Eclipse for Java EE development 2 45
activeMQ Queue Messages Enqueued and Messages Dequeued 1 39
List the words in Notepad++ 16 17
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question