简体   繁体   中英

Using regex to match beginning and end of string [Java]

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?

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

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

正则表达式可视化

This will match maze(any number).in

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

正则表达式可视化

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.

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.

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).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM