I found this regex in a plugin called hi5validator for jQuery, and I found it pretty good, I'm already using it on JavaScript:
/^([\+][0-9]{1,3}([ \.\-])?)?([\(][0-9]{1,6}[\)])?([0-9 \.\-]{1,32})(([A-Za-z \:]{1,11})?[0-9]{1,4}?)$/
I wanted to use this regex but in Java, and I tried to do this same thing with another regex in that library, but when I used an online evaluator, the expression gave lots of trouble. Fortunately, i found another regex that helped with that.
As for this one, can someone give me the proper Java version?
The logic of your regex is fine - you need to fix some minor details:
Put double quotes " instead of slashes / around your regex
Do not escape with back slashes parentheses ( ), dashes - in trailing positions, pluses +, colons :, and dots . inside character classes (I am not sure if it is necessary to escape these characters in Javascript either).
Here is what you should get:
"^([+][0-9]{1,3}([ .-])?)?([(][0-9]{1,6}[)])?([0-9 .-]{1,32})(([A-Za-z :]{1,11})?[0-9]{1,4}?)$"
Related
I work on a product that provides a Java API to extend it.
The API provides a function which
takes a Perl regular expression and
returns a list of matching files.
I want to filter the list to remove all files that end in .xml, .xsl and .cfg; basically the opposite of .*(\.xml|\.xsl|\.cfg).
I have been searching but I haven't been able to get anything to work yet.
I tried .*(?!\.cfg) and ^((?!cfg).)*$ and \.(?!cfg$|?!xml$|?!xsl$).
I don't know if I am on the right track or not.
Note
I know the regex systems are similar, but I can't get a Java regex working either.
You may use
^(?!.*\.(x[ms]l|cfg)$).+
See the regex demo
Details:
^ - start of a string
(?!.*\.(x[ms]l|cfg)$) - a negative lookahead that fails the match if any 0+ chars other than line break chars (.*) are followed with xml, xsl or cfg ((x[ms]l|cfg)) at the end of the string ($)
.+ - any 1 or more chars other than linebreak chars. Might be omitted if the entire string match is not required (in some tools it is required though).
You need something like this, which matches only if the end of the string isn't preceded by a dot and one of the three unwanted types
/(?<!\.(?:xml|xsl|cfg))\z/
I have a Lua editor in which I implemented syntax highlighting. I use regexes to match expressions like strings, comments, tokens, numbers, etc of Lua. The whole thing is made in Java and uses Java regexes. I had trouble with two things:
Multiline strings - Lua multiline brackets start and end with double square brackets [[ Everything between is the string, there can even be nested multiline strings. You can see what I made here, the regex is \[\[((?>[^\[\[\]\]]|(?R))*\]\]) and it works. It's similar to what you can see on this page under the match balanced constructs section. It finds expressions with equal amounts of [[ and ]] The thing is, recursion is not supported by Java regex engine. How can I replace it with something supported?
Multiline comments - Lua multiline comments start with --[====[ and end with ]====]. It ends only if there is as much equal signs as the opening bracket. There can be anywhere between 0 and infinite equal signs. I made this regex --\[\[((.|\n)*?)\]\] but it only works for the --[[ comment ]] pattern and do not support this --[==[ comment ]==]. Maybe I could do something like counting number of matches of equal signs at the opening then match the same the number for the closing tag. Is this possible in java regex? How?
Try this
--\[(=*)\[(.|\n)*?\]\1\]
Multiline string literals are absolutely the same but without leading --:
\[((=*)\[(.|\n)*?)\]\2\]
The string is something in the format:
[anything anything]
with a space separating the two, 'anything's.
I've tried:
(string).replaceAll("(^[)|(]$)","");
(string).replaceAll("(^\[)|(\]$)","");
but the latter gives me a compilation error and the first doesn't do anything. I implemented my current solution based on:
Java Regex to remove start/end single quotes but leave inside quotes
Looking around SO yields me many questions that answer problems similar to mine but implementing their solutions do not work (they either do nothing, or yield compilation errors):
regex - match brackets but exclude them from results
Regular Expressions on Punctuation
What am I doing wrong?
Since both Java and regex treats the \ character as an escape character, you actually have to double them when using in a Java literal string.
So the regular expression:
(^\[)|(\]$)
in a Java string actually should be:
"(^\\[)|(\\]$)"
I have escaped special characters and verified that the string passed to the Pattern is what I want.
I printed it on the screen and all double slashes were single again.
Particularly, I want these to be found:
\z.\s.\f.jtuy \z.yu \aw.o
lambda expressions. My regex is
(\\[a-z]{1,}\.){1,}[a-z]{1,}
and it - as I said - is working online. But why not in eclipse?
Do double backslashes get to the Pattern unchanged?
Is there any replacement for them?
Thanks.
If you mean "in the Java sourcecode" by saying "in Eclipse" you might need to use four backslashes: four backslashes will become two backslashes for the regex engine. You need to escape the backslash twice: once for the Java string and the second time for the regex engine.
I was trying to match the example in ,
<p>LinkToPage</p>
With rubular.com I could get something like <a href=\"(.*)?\/index.html\">.*<\/a>.
I'll be using this in Pattern.compile in Java. I know that \ has to be escaped as well, and I've come up with <a href=\\\"(.*)?\\\/index.html\\\">.*<\\\/a> and a few more variations but I'm getting it wrong. I tested on regexplanet. Can anyone help me with this?
Use ".*" in your Java code.
You only need to escape " because it's a Java string literal.
You don't need to escape /, because you aren't delimiting your regex with slashes (as you would be in Ruby).
Also, (.*)? makes no sense. Just use (.*). * can already match "nothing", so there's no point in having the ?.
Pattern.compile(".*");
That should fix your regex. You do not need to escape the forward slashes.
However I am obligated to present you with the standard caution against parsing HTML with regex:
RegEx match open tags except XHTML self-contained tags
You can tell Java what to match and call Pattern.quote(str) to make it escape the correct things for you.