I'm trying to figure out a regex pattern to use with the Java String.replaceAll() function. I need to replace all the %26 but I don't want it to pick up any other numbers with a '26' prefix.
For example I want:
"abc%26def".replaceAll(regex, "&") to return "abc&def"
- and -
"abc%2623def".replaceAll(regex, "&") to return "abc%2623def" (no change)
I'm aware I can easily write a few more lines of code to accomplish this task but I was wondering if it's possible to do this with just a single replaceAll.
You can use a negative lookahead assertion that prevents matches where %26 is followed by another digit (you'll need to escape the \ in Java, so it would be \\d):
%26(?!\d)
Regex Demo: http://www.rubular.com/r/J07zojxabd
Java Demo: http://ideone.com/luCmFN
You could achieve this using negative lookahead. From the manual:
(?!X) X, via zero-width negative lookahead
You appear to be doing decoding of percent-encoded octets, in which case you might want to look at URLDecoder.decode:
System.out.println(URLDecoder.decode("abc%26def", "UTF-8")
This may or may not work for your purposes, as it also translates + to a space.
Related
Considering this string below, I want to split a string on a single quote not preceded by \ and not followed by another single quotes('') with regex.
Note: In a string below, those are two consecutive single quotes not double quote.
Java is my 'favorite\''\'' prog' language
I used
split("(?<!\\\\)'")
but does not work. Instead it also takes one quote in every consecutive single quotes preceded by back slash
I want this output
Java is my
favorite\''\'' prog
language
Take a look at lookbehind / lookahead
("(?<![\\\\'])'(?!')")
For this example you also might match a single ' quote and a word boundary \b and the other way around:
'\b|\b'
split("'\\b|\\b' ")
Example Java
Or with a negative lookbehind and negative lookahead taking the whitespace into account:
(?<![\\'])' ?(?!')
split("(?<![\\\\'])' ?(?!')")
Example Java
Credits to #Graciano for the setup of the solution.
From your example I suggest using something similar to
split("[^\\\\']'")
but be careful with corner cases.
I would like to replace the following string.
img/s/430x250/
The problem is there are variations, like:
img/s/265x200/
or:
img/s/110x73/
So I would like to replace this part in whole, but the numbers are changeable, so how could I make a pattern that replaces it from a string?
Is your goal to match all three of those cases?
If so, this should work: img\/s\/\d+x\d+\/
It searches for img/s/[1 or more digits]x[1 or more digits]/
This regular expression will match your examples
img\/s\/\d+?x\d+?\/
the / matches /
the \d matches digits 0-9 and the + means 1 or more. The ? makes it lazy instead of greedy.
the img and s just match that literally
check out https://regex101.com/ to try out regular expressions. It's much easier than testing them by debugging code. Once you find an expression that works, you can move on to make sure your specific code will perform the same.
I'm looking for a regular expression for Java to find digits structured like this:
XXYXX or XYYYX
So results could be 66266 or 71117
Thanks for your help!
Try this regex:
input.matches("((\\d)\\1\\d\\1\\1|(\\d)(\\d)\\3\\3\\2)");
It uses back references to handle repeating numbers and the regex "or (A|B)
Note that this regex will match 99999, which is allowable by your definition (ie X and Y may be the same digit).
Also note the escaped back slashes \\ for specifying a single backslash in the regex in a java String.
I'd suggest (\d)\1\d\1\1 for the first case and (\d)(\d)\2\2\1 for the second. Mixing them both with non-capturing groups:
(?:(\d)\1\d\1\1)|(?:(\d)(\d)\2\2\1)
Not sure how Java plays with Regex though.
I have a String, and I want to replace it:
src="test.jpg" -> src="file://test.jpg"
src="http://xxx...." -> untouched
In fact I replace src=" with src="file:// but I don't want to replace it if it starts with http, e.g. src="http.
So I wrote this regexp to replace src=" with src="file://:
html2.replaceAll("src=\"","src=\"file://");
But the problem is that this also matches src="http.
I didn't know how to build the regexp for this. I thought that I can make it like this, but it doesn't work:
html2.replaceAll("src=\"[^(http)]","src=\"file:///android_asset/verkehr/");
I think you want a zero width negative lookahead.
html2.replaceAll("(src=\"(?!http://))", "src=\"file:///");
But beware of other protocols such as https, ftp etc.
you want a negative look ahead.
html2.replaceAll("src=\"(?!http)",,....
Use regex with negative lookahead:
src=\"(?!http://)
I am trying to write a regular expression to do a find and replace operation. Assume Java regex syntax. Below are examples of what I am trying to find:
12341+1
12241+1R1
100001+1R2
So, I am searching for a string beginning with one or more digits, followed by a "1+1" substring, followed by 0 or more characters. I have the following regex:
^(\d+)(1\\+1).*
This regex will successfully find the examples above, however, my goal is to replace the strings with everything before "1+1". So, 12341+1 would become 1234, and 12241+1R1 would become 1224. If I use the first grouped expression $1 to replace the pattern, I get the wrong result as follows:
12341+1 becomes 12341
12241+1R1 becomes 12241
100001+1R2 becomes 100001
Any ideas?
Your existing regex works fine, just that you are missing a \ before \d
String str = "100001+1R2";
str = str.replaceAll("^(\\d+)(1\\+1).*","$1");
Working link
IMHO, the regex is correct.
Perhaps you wrote it wrong in the code. If you want to code the regex ^(\d+)(1\+1).* in a string, you have to write something like String regex = "^(\\d+)(1\\+1).*".
Your output is the result of ^(\d+)(1+1).* replacement, as you miss some backslash in the string (e.g. "^(\\d+)(1\+1).*").
Your regex looks fine to me - I don't have access to java but in JavaScript the code..
"12341+1".replace(/(\d+)(1\+1)/g, "$1");
Returns 1234 as you'd expect. This works on a string with many 'codes' in too e.g.
"12341+1 54321+1".replace(/(\d+)(1\+1)/g, "$1");
gives 1234 5432.
Personally, I wouldn't use a Regex at all (it'd be like using a hammer on a thumbtack), I'd just create a substring from (Pseudocode)
stringName.substring(0, stringName.indexOf("1+1"))
But it looks like other posters have already mentioned the non-greedy operator.
In most Regex Syntaxes you can add a '?' after a '+' or '*' to indicate that you want it to match as little as possible before moving on in the pattern. (Thus: ^(\d+?)(1+1) matches any number of digits until it finds "1+1" and then, NOT INCLUDING the "1+1" it continues matching, whereas your original would see the 1 and match it as well).