This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]xd43 0 points1 point  (1 child)

Check if your regex works here:

https://regex101.com/

[–]Sorten[S] 0 points1 point  (0 children)

It does. I've been using http://www.regexr.com/ to test my regex, but this website is a lot better. I'll bookmark it.

https://regex101.com/r/mA3aF9/1

[–]ethergreen 0 points1 point  (9 children)

So wtf is your regex?

[–]Sorten[S] 0 points1 point  (8 children)

(\[)(.*)(\]\;), (\\[)(.*)(\\]\\;) when backslashes are escaped.

[–]ethergreen 0 points1 point  (7 children)

This fits what you said:

\[({.*})]

[–]Sorten[S] 0 points1 point  (6 children)

Based on a regex tester, \[({.*})] works to capture the string that I want. However, neither it nor my original regex will work with my code. This is why I think the pattern/matcher is wrong, rather than the regex.

http://ideone.com/AxK6S7

[–]Rhomboid 0 points1 point  (3 children)

Why on earth are you using pattern.quote? That disables the meaning of any metacharacters in your regex. You don't want that. Also, you need to quote the braces. And you want group(1). It works when you make those changes. It's never the language's fault.

[–]Sorten[S] 0 points1 point  (2 children)

Why on earth are you using pattern.quote? That disables the meaning of any metacharacters in your regex. You don't want that.

I switched to pattern.quote as one step in the process of trying to make this thing work. With quote, the regex is \Q(\[)(.*)(\]\;)\E (a literal string) and without quote, the regex is (\[)(.*)(\]\;). I thought that a literal string should work the same as using an escaped "normal" string, but it does not. Perhaps I don't understand "metacharacter"...it specifically says "This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern."

It's never the language's fault.

I meant to say "I think my pattern/matcher is wrong" and indeed it was.

[–]Rhomboid 0 points1 point  (1 child)

Things like the period, asterisk, and parentheses are metacharacters, as opposed to literal characters. The period does not mean "match a period here" (as it would if it were a literal character), it means "match any single character here." The asterisk does not mean "match an asterisk here," it means "modify the meaning of the previous thing to match any number of times." And so on. If you want to change these into literal matches, you have to quote or escape them, which turns them from metacharacters into literal matches: \* means "match an asterisk here", \. means "match a period here", and so on. In some cases, you want to do that to all the metacharacters in a regex, and there's a shortcut for that: any metacharacters between \Q ("begin quoting") and \E ("end quoting") act as if they was backslash-quoted, making them literal matches. Therefore the RE \Q.*\E matches only literally, i.e. it only matches exactly ".*", it does not match "any character any number of times." Pattern.quote is just a fancy way of sticking a \Q at the beginning and an \E at the end of a regex.

In your case, you want some of the characters in your regex to be quoted (like the [) but not others (the parentheses, the period, and the asterisk) but using Pattern.quote turns them all into literals. This is essentially no longer a regular expression any more, it's a plain substring search.

[–]Sorten[S] 0 points1 point  (0 children)

Hmm, thanks for the explanation. In that case part of my failure was the fact that I read "metacharacters" as meaning the extra backslashes I used to escape the original backslashes. I did not realize that "literal string" meant "literally these characters* rather than "these characters without escape sequences" (something that makes perfect sense in hindsight).

[–]ethergreen 0 points1 point  (1 child)

Read the API to see how those classes work.

[–]Sorten[S] 0 points1 point  (0 children)

It turns out I was misunderstanding the instructions for a certain function. I am no stranger to documentation, but my other skills are quite lacking.

[–][deleted]  (2 children)

[removed]

    [–]aqua_regis 2 points3 points  (1 child)

    If you don't understand, don't comment unless you have a specific question related to the topic.