0

i am not much familiar with regex expressions use in Java. If i have a String this/is/a/file!/path

now with substring and indexOf i can find the name file appearing inbetween / and ! but i am pretty sure such tasks must be much easier using regexes.

Can someone give me an example for doing so using regex expressions?

5
  • For this problem, your approach is the simplest yet the fastest. Don't think of RegEx unless you want to read something apart from the file part of the string. Commented Jan 12, 2012 at 10:39
  • Google is your friend. docs.oracle.com/javase/tutorial/essential/regex Commented Jan 12, 2012 at 10:40
  • @adarshr: How would you do this without regex? Seems to me it's just complicated enough that regex is the simplest way. Commented Jan 12, 2012 at 12:05
  • @AlanMoore, something like this: img269.imageshack.us/img269/4510/searchzy.png Commented Jan 12, 2012 at 13:05
  • Yeah, that's about the best I could come up with, too. The regex approach is shorter and easier to read. Commented Jan 12, 2012 at 14:43

4 Answers 4

3

Another way is to use Pattern and Matcher. Here you can use groups and more complex operations

Pattern pattern = Pattern.compile(yourRegex);
Matcher matcher = pattern.matcher(yourText);
while(matcher.find()) {
   System.out.println(matcher.group(1));
}

Take a look at

Matcher : http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Matcher.html

Pattern : http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html

Regex in Java : http://docs.oracle.com/javase/tutorial/essential/regex/

Sign up to request clarification or add additional context in comments.

Comments

0

somthing as .*/([^!]*)!.* should do this... then you can use a matcher to find the 1st group (the part between parenthesis, the 0th group is the whole match)

I didn't test this solution...

Comments

0

You can create a Pattern Object with a regular expression and then create a matcher object from it to parse your String.

String regex = ".*/(.*)!";

Pattern p = Pattern.compile (regex);

Matcher m = p.matcher(StringToBeMatched);

if (m.find()) {

    String filename = m.group (1);
}

1 Comment

You didn't test this solution, did you? The code is correct, but that regex won't even compile.
-1

The simplest way of using them in Java is by using the String.matches method:

boolean matches = "abc".matches("[a-z]+");

That would yield true.

The second way of doing it is useful if you have to use a specific regex pattern a lot of times. You can compile it and reuse it:

Pattern pattern = Pattern.compile("[a-z]+");
Matcher matcher = pattern.matcher("abc");
boolean matches = matcher.matches();

1 Comment

String.matches would only match the entire string. The question refers to extracting a matching substring of the path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.