Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-03-2012, 11:08 PM   PM User | #1
billatl
New Coder

 
Join Date: Oct 2009
Posts: 51
Thanks: 6
Thanked 0 Times in 0 Posts
billatl is an unknown quantity at this point
Using String "Matches" with Variable

Hello,

I'm trying to use matches to find a string within a string, but running into trouble using it with a variable. Concatenating the string doesn't seem to be the answer - what is the correct syntax?

Code:
String queryString = request.getQueryString();
for(String value: mvalues ) {
  if (value.matches("(?i).*" + queryString + ".*")) {
	out.println(value + " is a match!");  
  }  
}
billatl is offline   Reply With Quote
Old 10-05-2012, 07:03 PM   PM User | #2
billatl
New Coder

 
Join Date: Oct 2009
Posts: 51
Thanks: 6
Thanked 0 Times in 0 Posts
billatl is an unknown quantity at this point
No one uses matches with a variable?
billatl is offline   Reply With Quote
Old 10-05-2012, 09:39 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I'm not sure I follow you here.
That above will work fine.
PHP Code:
String str "anything /SOMETHING/ done";
String queryString "/something/";
String[] str.split(" ");
for (
String s a)
{
    if (
s.matches("(?i).*" queryString ".*"))
    {
        
System.out.println(" is a match.");
    }

Will result in:
Code:
/SOMETHING/ is a match
So since the structure works, it's a problem with the pattern not matching the data.
Fou-Lu is offline   Reply With Quote
Old 10-13-2012, 09:34 PM   PM User | #4
billatl
New Coder

 
Join Date: Oct 2009
Posts: 51
Thanks: 6
Thanked 0 Times in 0 Posts
billatl is an unknown quantity at this point
Fou-Lu,

Thanks, you are correct in that it's the pattern; it seems the matches isn't finding the substring anywhere. For example, the 1st works - but teh second doesn't:

Code:
String str = "anything /SOMETHING/ done";
String tstr = "something";

if (tstr.matches("(?i).*/something/.*")) {
        out.println(tstr + " text found.");
    } 

if (tstr.matches("(?i).*anything /SOMETHING/ done.*")) {
    out.println(tstr + " text found.");
}

In your example, you used the split to make it work - I can't do so for my purposes because the value I'm searching through won't have any spaces.

Can you tell what's wrong with the matching regex?
billatl is offline   Reply With Quote
Old 10-14-2012, 05:50 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I split mine up simply so I'd have an input loop to work with as you had in your original as well.
What are you matching and pattern you are attempting to match with?
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:04 PM.


Advertisement
Log in to turn off these ads.