简体   繁体   中英

why does this regex not work?

I am trying to match a string with a java regex and I cannot succeed. I'm pretty new to java and with most of my experience being linux based regex, I've had no success. Can someone help me?

Below are the codes that Im using.

The regex is-

//vod//final\_\d{0,99}.\d{0,99}\\-Frag\d{0,99}

The line that I'm trying to match is

/vod/final_1.3Seg1-Frag1

where I want 1.3, 1 and 1 to be wildcarded.

Someone please help me out... :(

This should work:

Pattern p = Pattern.compile( "/vod/final_\\d+\\.\\d+Seg\\d+-Frag\\d+" );

Notes: To protect special characters, you can use Pattern.quote()

When running into problems like this, start with a simple text and pattern and build from there. Ie first try to match / , then /vod/ , then /vod/final_1 , etc.

You are missing the Seg1 part. Also you are escaping characters that need not to be escaped. Try out this regexp: /vod/final_\\\\d+\\\\.\\\\d+Seg1-Frag\\\\d+

You're escaping too much. Don't escape /, _, -.

Something like:

/vod/final_\d{0,99}.\d{0,99}-Frag\d{0,99}

Does this work?

/\/vod\/final\_\d{0,99}.\d{0,99}Seg\d-Frag\d{0,99}

Also, here's what I used to edit the regex you provided above: http://rubular.com/

It says it's for ruby, but it also mentions that it works for java too.

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