0

I'm looking for a reg expression which has the exact same meaning as the "*" operator in a linux / windows command line. For example, find all files that: starts with 0 or more random chars, contains "abc" in the middle, and ends with 0 or more random chars.

So something like this in Java:

if (test.match("*abc*"))
    System.out.println("found match");

3 Answers 3

5

Original answer:

.*abc.*

Is the regexp which solves your problem. Note that if you want to match newline as part of your test string, you might need to enable single-line mode.

Revised answer if you are really talking about files:

[^/]*abc[^/]*

is a better answer since globs do not actually match directories in "*". For example, /etc/*bar will match /etc/foobar but will not match /etc/foo/bar. However, you said you were not interested in filenames, so the difference may be irrelevant to you.

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

2 Comments

That was basically what I came up with too, except that I used the brackets like Vivien mentioned; thought I'd ask here to make sure I was doing it right :)
@user85116: Answer revised to discuss globs not matching directories, though you may not care.
2

* in Unix is expressed as (.*) in regular expressions.

if (test.match("(.*)abc(.*)")) { /* ... */ }

3 Comments

The parentheses are optional. They signify making it a matching group, which may or may not be wanted.
Adam, can you explain your comment a little more?
Anything in parens captures the contents, so you can in java find out exactly what the prefix and suffix of abc is.
1

Sounds like you want to 'glob' more than a full regex. Check this page out http://download.oracle.com/javase/tutorial/essential/io/find.html

1 Comment

Thanks Andrew; I just used the file concept to explain what I wanted out of the * I'm not actually looking for files :)

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.