Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2010

Tip: Looking for answers? Try searching our database.

Regular expression in Java

Thread view: 
The87Boy - 03 Feb 2010 14:15 GMT
I am trying to create a new profile management system, and I have some
regular expression I used in PHP e.g. "<\/b>&nbsp;(.*?) aar"
Where I get the String between &nbsp; and aar
Is there any way I can do this in Java?
Eric Sosman - 03 Feb 2010 15:11 GMT
> I am trying to create a new profile management system, and I have some
> regular expression I used in PHP e.g. "<\/b>&nbsp;(.*?) aar"
> Where I get the String between&nbsp; and aar
> Is there any way I can do this in Java?

    There's a splendid new on-line system for answering this
kind of question.  Visit http://www.google.com/ (that's two O's
and one G, not the other way around) and enter a search for, oh,
gee, I dunno, maybe "Regular expression in Java".  Chances are
Very Good Indeed that they'll have something you can make use of.

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Lew - 03 Feb 2010 16:28 GMT
The87Boy wrote:
>> I am trying to create a new profile management system, and I have some
>> regular expression I used in PHP e.g. "<\/b>&nbsp;(.*?) aar"
>> Where I get the String between&nbsp; and aar
>> Is there any way I can do this in Java?

>     There's a splendid new on-line system for answering this
> kind of question.  Visit http://www.google.com/ (that's two O's
> and one G, not the other way around) and enter a search for, oh,
> gee, I dunno, maybe "Regular expression in Java".  Chances are
> Very Good Indeed that they'll have something you can make use of.

You mean, like
<http://java.sun.com/javase/6/docs/api/java/util/regex/package-frame.html>
?

The API Javadocs are very useful.

Signature

Lew

The87Boy - 03 Feb 2010 16:33 GMT
> The87Boy wrote:
> >> I am trying to create a new profile management system, and I have some
[quoted text clipped - 10 lines]
> <http://java.sun.com/javase/6/docs/api/java/util/regex/package-frame.html>
> ?

Yes, but I don't know I should make use of it in my situation
Lew - 03 Feb 2010 17:38 GMT
>>>> I am trying to create a new profile management system, and I have some
>>>> regular expression I used in PHP e.g. "<\/b>&nbsp;(.*?) aar"
>>>> Where I get the String between&nbsp; and aar
>>>> Is there any way I can do this in Java?

Yes.

Eric Sosman wrote:
>>>     There's a splendid new on-line system for answering this
>>> kind of question.  Visithttp://www.google.com/(that's two O's
>>> and one G, not the other way around) and enter a search for, oh,
>>> gee, I dunno, maybe "Regular expression in Java".  Chances are
>>> Very Good Indeed that they'll have something you can make use of.

Lew wrote:
>> You mean, like
>> <http://java.sun.com/javase/6/docs/api/java/util/regex/package-frame.html>
>> ?

Did you read this link?  Have you read through the descriptions of the various
types and methods in that package?

> Yes, but I don't know I should make use of it in my situation

Neither do we.  You haven't seen fit to share any details.  What exactly is
your situation?

I should think you'd make use of it in exactly parallel a way to how you use
the PHP equivalent.

If you can't share these details, then Eric's advice that GIYF, and reading
the Javadocs, and reaching conclusions from there is really the only way to
go.  Have you tried this?

Part of the essential skill set of the competent programmer is to reason how
to use available APIs, to assemble them into the logic one wishes to embody.

What is the logic you wish to embody?
What are your specific questions about the Java regex libraries?
What have you tried so far?
What results has that achieved?

Read <http://sscce.org/>

GIYF.  JDAYF.

Signature

Lew

The87Boy - 03 Feb 2010 18:13 GMT
> >>>> I am trying to create a new profile management system, and I have some
> >>>> regular expression I used in PHP e.g. "<\/b>&nbsp;(.*?) aar"
[quoted text clipped - 16 lines]
> Did you read this link?  Have you read through the descriptions of the various
> types and methods in that package?

Yes I have

> > Yes, but I don't know I should make use of it in my situation
>
> Neither do we.  You haven't seen fit to share any details.  What exactly is
> your situation?

I use the regular expression "<\/b>&nbsp;(.*?) aar" in PHP to get the
String between the space and aar (the .*)
I want to use it in the same way in Java
I could of course split the String up 2 times

> What are your specific questions about the Java regex libraries?

Whatever it is possible to find a String from another String by a
regular expression in Java

> What have you tried so far?

I have tried many thing, but it seems that I have to split the String
Knute Johnson - 03 Feb 2010 18:37 GMT
>>>>>> I am trying to create a new profile management system, and I have some
>>>>>> regular expression I used in PHP e.g. "<\/b>&nbsp;(.*?) aar"
[quoted text clipped - 34 lines]
>
> I have tried many thing, but it seems that I have to split the String

import java.util.regex.*;

public class test {
    public static void main(String[] args) {
        String str =
 "Some where over the </b>&nbsp;There is a duck aar from here to there";
        Pattern p = Pattern.compile("</b>&nbsp;(.*?) aar");
        Matcher m = p.matcher(str);
        if (m.find())
            System.out.println(m.group(1));
    }
}

You don't have to split the string.  I don't use PHP but it looks like
you need to escape the forward slash?  That is not required in Java.

Look at Pattern and Matcher in the docs.

Signature

Knute Johnson
email s/nospam/knute2010/

The87Boy - 03 Feb 2010 18:48 GMT
On 3 Feb., 19:37, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> >>>>>> I am trying to create a new profile management system, and I have some
> >>>>>> regular expression I used in PHP e.g. "<\/b>&nbsp;(.*?) aar"
[quoted text clipped - 53 lines]
>
> Look at Pattern and Matcher in the docs.

Oh, it was because I have to use Matcher's group with 1 instead of 0
Lothar Kimmeringer - 03 Feb 2010 18:52 GMT
> You don't have to split the string.  I don't use PHP but it looks like
> you need to escape the forward slash?  That is not required in Java.

If PHP is similar to Perl, the slash indicates a regular
expression, but you can change that to something else as well.

One additional note. If you want to escape a special character
you have to escape the escape-character in the String, so
Pattern.compile("<\/b>&nbsp;(.*?) aar");
wouldn't work,
Pattern.compile("<\\/b>&nbsp;(.*?) aar");
is to be used instead. Otherwise the compiler most likely
complains or - even more bad - creates a wrong regex.

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

The87Boy - 03 Feb 2010 19:41 GMT
On 3 Feb., 19:52, Lothar Kimmeringer <news200...@kimmeringer.de>
wrote:
> > You don't have to split the string.  I don't use PHP but it looks like
> > you need to escape the forward slash?  That is not required in Java.
[quoted text clipped - 9 lines]
> is to be used instead. Otherwise the compiler most likely
> complains or - even more bad - creates a wrong regex.

I think the right pattern is "</b>&nbsp;(.*?) aar" as we are taking
about the HTML-tag </b>
Roedy Green - 04 Feb 2010 20:45 GMT
>I am trying to create a new profile management system, and I have some
>regular expression I used in PHP e.g. "<\/b>&nbsp;(.*?) aar"
>Where I get the String between &nbsp; and aar
>Is there any way I can do this in Java?

for a quick primer in Java Regexes, see
http://mindprod.com/jgloss/regex.html

In Java ( .. ) capture the expression inside.  .* means anything.

See http://mindprod.com/jgloss/regex.html#FINDING for the code to
extract the field.

Signature

Roedy Green Canadian Mind Products
http://mindprod.com

You can’t have great software without a great team, and most software teams behave like dysfunctional families.
~ Jim McCarthy

The87Boy - 05 Feb 2010 22:19 GMT
On 4 Feb., 21:45, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:

> >I am trying to create a new profile management system, and I have some
> >regular expression I used in PHP e.g. "<\/b>&nbsp;(.*?) aar"
[quoted text clipped - 4 lines]
>
> In Java ( .. ) capture the expression inside.  .* means anything.

I can't get regex this to work:
</b> ((\\d)?) aar

But now this works:
</b> (.*?) aar

Can anybody explain me why?
Eric Sosman - 05 Feb 2010 23:05 GMT
> I can't get regex this to work:
> </b>  ((\\d)?) aar
[quoted text clipped - 3 lines]
>
> Can anybody explain me why?

    Not until you explain what you mean by "works."  Show
your code, show some of the strings you're trying to match,
tell us what you wanted to happen, and tell us what happened
instead.

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Roedy Green - 06 Feb 2010 07:26 GMT
>I can't get regex this to work:
></b> ((\\d)?) aar
[quoted text clipped - 3 lines]
>
>Can anybody explain me why?

You need to post real code since the devil is in the details.

I presume your regex actually was:

</b> ((\d)?) aar

In Java that was

Pattern.compile( "</b> ((\\d)?) aar" );

You wanted it to match:

"</b> 8 aar"
or
"</b>  aar" (2 spaces)

Is this correct?

Signature

Roedy Green Canadian Mind Products
http://mindprod.com

You can’t have great software without a great team, and most software teams behave like dysfunctional families.
~ Jim McCarthy

The87Boy - 09 Feb 2010 19:20 GMT
On 6 Feb., 08:26, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:

> >I can't get regex this to work:
> ></b> ((\\d)?) aar
[quoted text clipped - 21 lines]
>
> Is this correct?

Yes that is completely right the first you wrote :D
Roedy Green - 11 Feb 2010 00:51 GMT
>((\\d)?)

you could write that as [ 0-9]

Probably your problem is in the code you did not post. try composing
an SSCCE.  See http://mindprod.com/jgloss/sscce.html
and http://mindprod.com/jgloss/regex.html for some skeletons you could
use to hang your code.
Signature

Roedy Green Canadian Mind Products
http://mindprod.com

Every compilable program in a sense works. The problem is with your unrealistic expections on what it will do.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.