21

I want to search for a given string pattern in an input sting.

For Eg.

String URL = "https://localhost:8080/sbs/01.00/sip/dreamworks/v/01.00/cui/print/$fwVer/{$fwVer}/$lang/en/$model/{$model}/$region/us/$imageBg/{$imageBg}/$imageH/{$imageH}/$imageSz/{$imageSz}/$imageW/{$imageW}/movie/Kung_Fu_Panda_two/categories/3D_Pix/item/{item}/_back/2?$uniqueID={$uniqueID}"

Now I need to search whether the string URL contains "/{item}/". Please help me.

This is an example. Actually I need is check whether the URL contains a string matching "/{a-zA-Z0-9}/"

urbananimal
  • 495
  • 3
  • 9
skmaran.nr.iras
  • 8,152
  • 28
  • 81
  • 116

4 Answers4

40

You can use the Pattern class for this. If you want to match only word characters inside the {} then you can use the following regex. \w is a shorthand for [a-zA-Z0-9_]. If you are ok with _ then use \w or else use [a-zA-Z0-9].

String URL = "https://localhost:8080/sbs/01.00/sip/dreamworks/v/01.00/cui/print/$fwVer/{$fwVer}/$lang/en/$model/{$model}/$region/us/$imageBg/{$imageBg}/$imageH/{$imageH}/$imageSz/{$imageSz}/$imageW/{$imageW}/movie/Kung_Fu_Panda_two/categories/3D_Pix/item/{item}/_back/2?$uniqueID={$uniqueID}";
Pattern pattern = Pattern.compile("/\\{\\w+\\}/");
Matcher matcher = pattern.matcher(URL);
if (matcher.find()) {
    System.out.println(matcher.group(0)); //prints /{item}/
} else {
    System.out.println("Match not found");
}
Narendra Yadala
  • 9,554
  • 1
  • 28
  • 43
14

That's just a matter of String.contains:

if (input.contains("{item}"))

If you need to know where it occurs, you can use indexOf:

int index = input.indexOf("{item}");
if (index != -1) // -1 means "not found"
{
    ...
}

That's fine for matching exact strings - if you need real patterns (e.g. "three digits followed by at most 2 letters A-C") then you should look into regular expressions.

EDIT: Okay, it sounds like you do want regular expressions. You might want something like this:

private static final Pattern URL_PATTERN =
    Pattern.compile("/\\{[a-zA-Z0-9]+\\}/");

...

if (URL_PATTERN.matcher(input).find())
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for your immediate response. Now i edited the question. Could you please help me? – skmaran.nr.iras Oct 29 '11 at 09:34
  • 1
    @1355: Okay, so that's completely changed the nature of the question... but it's not really clear what you mean. Please give some examples of URLs you do and don't want to match. Is it "any number of letters/digits, but they have to be in braces"? – Jon Skeet Oct 29 '11 at 09:37
  • I need to check this pattern "/{a-zA-Z0-9}/". The pattern should include two slashes and two braces. But the letters can be anything. – skmaran.nr.iras Oct 29 '11 at 09:45
  • For eg: "/{asdFs7f}/", "/{asdfg}/", "/{ASDF}/", "/{23445}/". Now I think you got what i meant. In that URL string there are many sub strings like "/{$asdf}/". It shouldn't match. – skmaran.nr.iras Oct 29 '11 at 09:48
  • `.matcher()`. Not `.matches()`. – Det Jan 29 '22 at 21:43
  • @Det: Fixed - feel free to just edit typos directly in future though. – Jon Skeet Jan 29 '22 at 21:58
  • @Det: Ironically it looks like I originally got it right, and a different user "corrected` it to `matches()` (while fixing a separate, genuine issue). – Jon Skeet Jan 29 '22 at 21:59
  • @JonSkeet I don't know. I just don't like to edit other people's things. – Det Jan 30 '22 at 05:07
4

If you want to check if some string is present in another string, use something like String.contains

If you want to check if some pattern is present in a string, append and prepend the pattern with '.*'. The result will accept strings that contain the pattern.

Example: Suppose you have some regex a(b|c) that checks if a string matches ab or ac
.*(a(b|c)).* will check if a string contains a ab or ac.

A disadvantage of this method is that it will not give you the location of the match, you can use java.util.Mather.find() if you need the position of the match.

ChrisBlom
  • 1,262
  • 12
  • 17
2

You can do it using string.indexOf("{item}"). If the result is greater than -1 {item} is in the string

zeller
  • 4,904
  • 2
  • 22
  • 40