473,405 Members | 2,404 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,405 software developers and data experts.

Match a part of String using Reg Exp

dmjpro
2,476 2GB
Expand|Select|Wrap|Line Numbers
  1. String input = "Mozila";
  2. System.out.println(Pattern.matches("^Moz")); //returns false
  3. System.out.println(Pattern.matches("^Moz.+")); //returns true
  4.  
Can't i match part of the string using Reg Exp?
May 12 '10 #1
5 3498
jkmyoung
2,057 Expert 2GB
Interesting. I've never seen that before. Are you using capturing parentheses to get part of the match?
Assuming you wanted .* as opposed to .+
May 12 '10 #2
ahmee
36
there are different ways to go through a program....so go for the best u knwn
May 15 '10 #3
ahmee
36
this is the code...

import java.util.regex.*;
class Strtok {

public static void main(String [] args) {
String s= "Why John Smith and Alan Smith and Nick gates";
if(s.matches("Why John Smith and Alan Smith and Nick gates"))
{
// System.out.println(s.substring(4,14),s.substring(1 5,20));
String substring = s.substring(3,15);
String substring2 = s.substring(19,29);
String substring3 = s.substring(34,44);
System.out.println(substring);
System.out.println(substring2);
System.out.println(substring3);
}
}
}

output

John Smith
Alan Smith
Nick gates
May 15 '10 #4
dmjpro
2,476 2GB
What does your code mean to me?
May 17 '10 #5
jkmyoung
2,057 Expert 2GB
Probably misposted from a similar but different thread.
Assuming you are using a matcher, I would use parentheses to capture the input.
Expand|Select|Wrap|Line Numbers
  1. String input = "Mozila"; 
  2. Pattern pattern = Pattern.compile("^(Moz).*");
  3. Matcher matcher = pattern.matcher(input);
  4. while (matcher.find()){
  5.             System.out.println(matcher.group(1)); //prints out Moz
  6. }
Notice the use of 1 to get the first group as opposed to the entire match. Not entirely sure if this is what you want, but hope it leads you in the right direction.
May 17 '10 #6

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

Similar topics

5
by: Andrew Connell | last post by:
Having fits transforming an XML string using an XSL file. In the 1.1 version of the framework, I see that the XmlResolver is heavily used in the XslTransform class. However, that looks like I am...
12
by: babak | last post by:
Hi everyone I want to format a string (using sprintf) and put it in a messagebox but however I try to do it, it doesn't seem to work. Here is an example sample of what i try to do: char msg;...
2
by: Maziar Aflatoun | last post by:
Hi guys, I'm using Windows authentication to connect to SQL Server 2000. On my computer the connection is fine. Now if I move it to a remote server, how to I hard code my Username/Password in...
2
by: Bruno | last post by:
Hello friends I'm creating one application that will send content to another app using HTTP connection. To authenticate my connection, I need to send a string with the username and password...
3
by: minguskhan | last post by:
Does anyone know how to reverse a string using a loop?
2
by: aurora | last post by:
I have some unicode string with some characters encode using python notation like '\n' for LF. I need to convert that to the actual LF character. There is a 'unicode_escape' codec that seems to...
11
by: wreckingcru | last post by:
I'm trying to tokenize a C++ string with the following code: #include <iostream> #include <stdio> #include <string> using namespace std; int main(int argc, char* argv)
8
by: Nkhosinathie | last post by:
i'm comparing two string using the function stryncmp,but my problem is when i execute the program it doesn't give me the correct values.actually it gives me same results as the function strycmp ...
6
by: koutoo | last post by:
How do I delete or remove values from a list or string using the index. If a = and I want to get rid of 1 -5, how would I do that? Thanks.
2
by: bakpao | last post by:
Is there any way to decode a URL encoded string using XSLT? For example, this is the string that I have: This%20is%20the%20%3Ctable%3E%20tag And this is what I wanted XSLT to output: This is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.