\s will match those characters, so it consumes the \r, but that leaves. metacharacter won't match either of them. Both \r (carriage return) and (linefeed) are considered line-separator characters in Java regexes, and the. I suggest you look at some of the '\p' character classes in the Pattern class docs. The lines are probably separated by \r in your file. However, I think you'd be much better off supplying the allowed characters in a filename in the centre portion of the pattern, rather than just saying that it can be anything except a quote, otherwise you may end up matching a lot more than you bargained for. My suggestion would be a pattern something like: This regular expression w+(sw+) will only allow a single space between words and no leading or trailing spaces. pile("?:src)=(?:\"|'|`)?(.+)(?:\"|'|`)?", Pattern.CASE_INSENSITIVE) ignores the first character after the '=' or '"' (the CASE_INSENSITIVE parameter is also redundant)Īnd String bufferString = "" doesn't put quotes around your attributes. So, its better to use Pattern.quote as suggested here and not to reinvent the wheel. Java regex to split string with variable spaces and delimiter. What I'm looking for is the Line of Thought behind forming the regular expression. There are a lot of similar examples on this site, but the answers mostly give out the regex itself without explaining how it is constructed. Well, leaving aside my rant about filenames that contain spaces (which I still reckon is a bad idea), both your regex and your buffer look wrong to me: Pattern IMAGE_SOURCE_PATTERN = 'everything between them Q and E is considered as escaped' except other Q s and E s (which potentially may occur within original regex). Java regex split text (both the delimiter and the order may be unknown) 1. I am trying to construct this simple regex to match words + whitespace in Java, but I got confused trying to work it out. Ryan Raina wrote:I guess I have to modify regex.Could anybody shed light on it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |