473,416 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Using Regular Expression (regex) in Java Programming

Hiii all,

I'm new in using Regular Expression in Java.

For example, I have String s like this:

String s = "Why John Smith and Alan Smith and Nick Gates are the same?"

How can I get sub-strings as "John Smith", "Alan Smith", "Nick Gates" - (names of people - with first upper character) from s by using regex?

I try to use regex in Java, but It totally doesn't work.

This is my code:
Expand|Select|Wrap|Line Numbers
  1. /*
  2. String EXAMPLE_TEST = "Why John Smith and Alan Smith and Nick Gates are the same?";
  3. System.out.println(EXAMPLE_TEST.matches("([A-Z]&&[a-z])"));
  4. String[] splitString = (EXAMPLE_TEST.split("([A-Z]&&[a-z])"));
  5. System.out.println(splitString.length);
  6. for (String string : splitString) {
  7. System.out.println(string);
  8. }
  9.  
  10. */

Please help me to have exact "regex" in this case.

Thanks all in advance!
May 13 '10 #1

✓ answered by jkmyoung

I think it's something like:
"\ ([A-Z][a-z]+\ [A-Z][a-z]+)"

Added a space in the beginning so that you don't get "Why John" as your first match.

4 1852
jkmyoung
2,057 Expert 2GB
I think it's something like:
"\ ([A-Z][a-z]+\ [A-Z][a-z]+)"

Added a space in the beginning so that you don't get "Why John" as your first match.
May 13 '10 #2
hii jkmyoung,

I tried your regex. It seems to be worked, but It just prints out other words like this:
"Why,
and,
and,
are the same ?"

Can I print these words: "John Smith", "Alan Smith", "Nick Gates" ?
May 14 '10 #3
thanks jkmyoung very much!

I figured out by using matcher.group():

Pattern pattern = Pattern.compile(" [A-Z][a-z]+ [A-Z][a-z]+");
Matcher matcher = pattern.matcher("Why John Smith and Alan Smith and Nick Gates are the same?");
while( matcher.find() )
System.out.println("main: "+matcher.group());
May 14 '10 #4
pbrockway2
151 Expert 100+
If this is anything other than homework then you're going to have problems with the likes of Old MacDonald, Rip van Winkle and Stephen J Gould.
May 14 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ye Zhou | last post by:
Hi all, I am trying to convert a perl program into java and I got stuck in the regular expression part. It seems like java.util.regex couldn't help much. I checked with Apache regexp and didn't...
2
by: Babu Mannaravalappil | last post by:
Hi, I want to replace some words in my text files (actually transpose). For example, I have a whole lot of expressions (words) in my files as follows: TABLECUSTOMERS TABLEORDERS...
7
by: hungrymind | last post by:
Hi all, I am developing some control (textbox based), to validate inputs to that control I am using regular expression, where pattern is generated dynamically. I need to identify what all...
11
by: lucky | last post by:
hi, i got file which contains "----------------" in a line. the line only contains this data as a saperation. using regular expression i want to i detify the line contains that data and replace...
1
by: Zorik | last post by:
I am building a form in asp.net 2.0 On one of the textboxes, I don't want that the user will use the space character. How do I disallow space using regular expression validator (or other...
3
by: mattmao | last post by:
Hello everyone. This question is regarding ISBN number checking. I've done it with my own algorithm, which is pretty bad in consideration about the total lines of code. Now I want to improve...
1
by: durga2005 | last post by:
hi I m validating a textbox using regular expression validator and these controls are placed inside the update panel the validation is not working inside update panel
4
by: Velhari | last post by:
Hi all, I wrote the following Javascript function used to execute the Javascript codes from the ajax response which contains both html & javascript. It works fine, if the javascript codes from...
5
by: padmaneha | last post by:
How to omit replacement in somepart of text using regular expression? For ex: If i want to replace mumbai with mumbai/aaa.com but if i have an anchor tag <a...
5
by: gjain12 | last post by:
Hi All, I have a string in which I have to find all the commas except the one which is followed by "space". I have tried ",, but it finds the patterns that start with , and one character. e.g If...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.