-5

java regexp, what dose the "\+"mean in " [_A-Za-z0-9-\+]+ "? I know + means one or more than one, so what doese \+ mean?

Sufian Latif
  • 12,106
  • 3
  • 31
  • 68
guanghuiz
  • 1
  • 3
  • A character within a character class denotes a literal character. – devnull Apr 28 '14 at 03:15
  • 1
    @supernova: Welcome to Stack Overflow! Please consider bookmarking the our [Regular Expressions FAQ](http://stackoverflow.com/a/22944075/2736496) for future reference. In particular, check out the answers for [character classes:`[...]`](http://stackoverflow.com/a/1553171) and the list of online regex testers (in the bottom section) where you can try things out your self. – aliteralmind Apr 28 '14 at 03:19
  • The only correct answer to this question is one character long. Unfortunately, Stack Overflow won't let me post a single character answer. – Dawood ibn Kareem Apr 28 '14 at 03:24
  • @supernova The regexes were not showing correctly, I've edited you post to fix that. But you mentioned `\\+` in the question heading and `\\\+` in the question - that's inconsistent. – Sufian Latif Apr 28 '14 at 03:56
  • @0605002 - You seem to have changed the question completely. Why? – Dawood ibn Kareem Apr 28 '14 at 04:09
  • @DavidWallace Not completely, I've just replaced some double-quotes with backticks. See mark-down comparison in edit history. – Sufian Latif Apr 28 '14 at 04:12
  • @0605002 Well, the question showed one backslash previously. Now it shows three. So you've made it into a different question. That won't help supernova at all. I think it would be best if you put the question back how it was. – Dawood ibn Kareem Apr 28 '14 at 04:14
  • @DavidWallace but supernova did type three backslashes in the question, but that was not appearing properly. You can try it easily: edit the question, type multiple backslashes (not in a code block) and see the preview. Seems that the SO editor also needs escaped backslashes, but in a weird way. – Sufian Latif Apr 28 '14 at 04:18
  • @0605002 Presumably, supernova typed the right number of backslashes to get his/her question appearing the way he/she wanted it. You shouldn't go changing a question if (i) you are not the OP, and (ii) people have already started answering. Please undo your vandalism. – Dawood ibn Kareem Apr 28 '14 at 04:52

2 Answers2

1

None of the special characters works like that within a character class (i.e. characters enclosed between [ and ]). The regex would literally match the characters _, A to Z, a to z, 0 to 9, -, \ and +.

As this is a java string, the \ character needs to be escaped with another \.

Edit:

Just found out, backslashes need to be escaped within a character class. So, to match a \, it should be \\. Indeed \ remains a special character within a character class, that's why we can use things like \d, \w within character classes.

The regex should be [_A-Za-z0-9-\\+]+. See here.

Sufian Latif
  • 12,106
  • 3
  • 31
  • 68
  • Indeed. The original regex was written based on incorrect beliefs. – Ernest Friedman-Hill Apr 28 '14 at 03:20
  • I've just tested this. The regexp does not match a backslash - backslash is still special inside square brackets. This answer is incorrect. – Dawood ibn Kareem Apr 28 '14 at 03:33
  • @DavidWallace Yeah, you're right. I've edited my post. However, the question was not showing the regex correctly. – Sufian Latif Apr 28 '14 at 03:57
  • I've removed my downvote, because at this point, it's anybody's guess what the question was actually meant to be. I took it to mean two backslashes written in the literal, that is, one backslash in the actual regular expression - which means it wouldn't match backslash. However, now that there are three backslashes in the question, who knows? – Dawood ibn Kareem Apr 28 '14 at 04:06
  • It simply means "\+" two distinct characters \ and + since one extra slash is for escape – guanghuiz Apr 29 '14 at 03:05
0

It is not a accepted regexp, if you want to try it out IDE shows an error you can try this :

[_A-Za-z0-9-\\+]+]
Jérémie Bertrand
  • 2,917
  • 3
  • 41
  • 48