java regular expression pattern

Can someone show me regular expression syntax on how to search for the following pattern using the Pattern.compile() method

Pattern 1 = search for a line with one word only
Pattern 2 = search for a line with the "(some string)"
areyouready344Asked:
Who is Participating?
 
sanjeev_mcaCommented:
you can use \w character class to match any word ... for example

Pattern p =Pattern.compile("\w");
Matcher m = p.matcher(<any string>);

then you can check .. by calling m.matches() to check whether your string matches the regex or not

hope this helps ..
0
 
sanjeev_mcaCommented:
Can you please provide an example, as your question is not clearly stating what you need.
0
 
areyouready344Author Commented:
Example for pattern 1:

I want pattern 1 to search for the line with a single word, in the example below, it should find line 4.

1. as your question is not
2. as your question is not
3. as your question is not
4. question

Example for pattern 2:


I want pattern 2 to search for the line with (string) single word and omit it, in the example below, it should display lines 1, 3, and 4, and not line 2..

1. as your question is not
2. as your (question) is not
3. as your question is not
4. question
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
areyouready344Author Commented:
Correction Example for pattern 2:

Example for pattern 2:


I want pattern 2 to search for the line with "(string)", in the example below, it should display lines 1, 3, and 4, and not line 2..

1. as your question is not
2. as your (question) is not
3. as your question is not
4. question
0
 
sanjeev_mcaCommented:
did you try to do this using java ??? can you post your code for this?
0
 
areyouready344Author Commented:
I'm just trying to determine what's the regular expression syntax for Pattern.compile() method to accomplish example 1 and example 2.

Example 1 is important..

0
 
sanjeev_mcaCommented:
you can do it in multiple ways ... go through

http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

if you still find it difficult .. then let me know..
0
 
areyouready344Author Commented:
can you give me an example for Example 1?
0
 
areyouready344Author Commented:
to be more clear, can you provide an example for Example 1?
0
 
sanjeev_mcaCommented:
so you don't want to try it out for your self .. sorry my friend this is a help forum not an outsourcing agency.
0
 
areyouready344Author Commented:
you should have made a long story short by saying you're not going to answer my question or the question is invalid... idiot..
0
 
sanjeev_mcaCommented:
don't use such kind of language. i can't help a looser ...
0
 
areyouready344Author Commented:
I didn't know this was a web site for judging someone technical experience who is new to Java...
0
 
sanjeev_mcaCommented:
thts perfectly fine if you are new to java .. in that case you should not look for code .. you should learn by doing yourself. and you dont seem to do that.
0
 
areyouready344Author Commented:
You provided code but not the correct code that answer my question as it relates to Example 1. If this website can't provide me an example, I can try other web sites.
0
 
sanjeev_mcaCommented:
first thing .. that was a pseudo code and wrote to just give you an example of how to ... rest is up to you ...
0
 
areyouready344Author Commented:
you were not clear that is was pseudo code to someone who is new to Java.

I've tried the following but can't get it to work....

Pattern p = Pattern.compile("\\w+\\s+");

I'm sure any expert can figure this out...
0
 
areyouready344Author Commented:
anyone besides this Micky-mouse sanjeey_mca can help provide me an answer to Example 1...

I tried this but not working

 Pattern pattern = Pattern.compile("(\\w)(\\s{4})");
0
 
sanjeev_mcaCommented:
LOLs .. you called me micky mouse .. never mind.. so now .. it seems you tried something .. i am happy to help you buddy ..here is a example1

      String regex = ".*\\s+.*";
      
      String mulStr = "Multiple Word String";
      String singleStr = "SingleWord";
      
      System.out.println("1. " + mulStr.matches(regex));
      System.out.println("2. " + singleStr.matches(regex));

Now solve your 2nd example yourself ....
0
 
robthewolfCommented:
areyouready, did
".*\\s+.*";
work?

this looks like it has 1 extra \
with out the extra \ it should match 1,2,3 from example 1.
You need the inverse of this to match 4

I assume each line is a regular string with no additional white space
If so then try this for example 1 to match just line 4
"^\w+$"
Which means starts with (^) a letter or number or underscore (\w) atleast once (+) and nothing between the end of the letters until the end of the line ($)

if you want to pad it with white space
"^\s*\w+\s*$"
where \s* is 0 or more non printing characters.
this will mean that if there is a space before the word or after then it will still count.  It will not allow more than one word because of the ^ and $

for example 2 you can either find the inverse of this:

"\(\w+\)"

or more specific

"\s+\(\w+\)\s+"
where the \s+ add the requirement of spaces before and after the parentheses

alternatively you could match this:
"\s+^\(\w+^\)\s+"
where ^\( means not (
I am not 100% on this.  you will have to try it out and play with it.
note here that the ^ is not the same is in my first example where it means the beginning of a line

ignore sanjeev_mca you can ask for what ever you want.  Obviously if you are polite and have had a go it does help.  We tend to prefer helping people when they are stuck than just writing code for who ever wants it.  But you can always ask, and someone might be nice enough to help you.  I know from experience sometimes its just hard to get started.
0
 
robthewolfCommented:
did this help?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.