3

I have a list of files in a folder:

maze1.in.txt
maze2.in.txt
maze3.in.txt

I've used substring to remove the .txt extensions. How do I use regex to match the front and the back of the file name? I need it to match "maze" at the front and ".in" at the back, and the middle must be a digit (can be single or double digit).

I've tried the following

if (name.matches("name\\din")) {
    //dosomething
}

It doesn't match anything. What is the correct regex expression to use?

1
  • 1
    This might be useful when starting out with regular expressions debuggex.com Commented Aug 30, 2013 at 2:34

5 Answers 5

14

I'm a little confused what you are asking for in particular

    ^(maze[0-9]*\.in)$

Regular expression visualization

This will match maze(any number).in

 ^(maze[0-9]*\.in)\.txt$

Regular expression visualization

this will match maze(any number).in.txt -- excludes the .txt NO NEED FOR USING SUB STRING!

Edit live on Debuggex

The think i would be wary about as of right now is the capture groups... I'm not particularly sure what you are doing with this regex. However, I believe explaining capture groups could benefit you.

A capture group for instance is denoted by () this is basically store them in the pattern array and is a way to parse stuff.

example maze1.in.txt

So if you want to capture the entire line minus .txt i would use this ^(maze[0-9]*\.in\.txt)$

However, if I wanted to capture things separately I would do this ^(maze)([0-9]*)(\.in)\.txt$ this will exclude .txt but include maze, the number, and .in IN separate indexes of the pattern array.

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

1 Comment

You need the ^, or $ if using String.matches(). It automatically tries to match the entire string (yeah this isn't documented).
2

Your original solution doesn't work because string "name" is not in your text. It is "maze".
You can try this

name.matches("maze\\d{1,2}\\.in")

d{1,2} is used to match a digit(can be single or double digit).

Comments

1

You need regex anchors that tell the regex to

start at the beginning: ^

and signal the end of the string: $

^maze[\d]{0,2}\.in$

or in Java:

name.matches("^maze[\\d]{0,2}\\.in$");

Also, your regex wasn't matching strings with a dot (.) which would not accept your examples given. You need to add \. to the regex to accept dots because . is a special character.

1 Comment

Actually that isn't required for String.matches(). It automatically tries to match the whole string as if ^ and $ were there.
0

It is always good to think of what you are trying to do in english, before you create regular expressions.

You want to match a word maze followed by a digit, followed by a literal period . followed by another word.

word   `\w` matches a word character
digit  `\d` matches a single digit
period `\.` matches a literal period
word   `\w` matches a word character

putting it all together into a single string you get (keep in mind the double backslash for the Java escape and the pluses to repeat the previous match one or more times):

"\\w+\\d\\.\\w+"

The above is the generic case for any file name in the format xxx1.yyy, if you wanted to match maze and in specifically, you can just add those in as literal strings.

"maze\\d+\\.in"

example: http://ideone.com/rS7tw1

Comments

0
name.matches("^maze[0-9]+\\.in\\.txt$")

Comments

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.