This question already has answers here:
How to escape text for regular expression in Java?
(8 answers)
Closed 4 years ago.
I have searched a lot about how to ignore part of RegEx expression. Here's why.
RegEx example:
".*atan2\\(.*\\).*"
It will detect the function 'atan2' like so:
string.matches("return atan2(45, 23)")
But as I bring in another String:
string.matches("return atan2("+Integer.valueOf(XInString)+", 58)")
And if XInString looks like that: 45.7 regex will be affected of that dot, because regex will look like that then: "return atan2(45.7, 58)" and the point between 5 and 7 is a part of regex. Is there a way to ignore that part?
For example:
string.matches("return atan2([45.7], 58)")
(The string between [ and ] is what i don't want regex to read)
...the point between 5 and 7 is a part of regex. Is there a way to
ignore that part?
Yes, you can escape that dot using Pattern.quote(XInString) like this :
"return atan2(" + Pattern.quote(XInString) + ", 58)"
Related
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I want to replace &sp; in the string below with Z.
Input text : ABCD&sp;EF&p;GHIJ&bsp;KL
Output text : ABCDZEFZGHIZKL
Can anyone tell me how to replace the every instance of &\D+; using java regular expression?
I am using /(&\D+;)?/ but it doesn't work.
Use String#replaceAll.
You also should use the ? modificator to +:
String str = "ABCD&sp;EF&p;GHIJ&bsp;KL";
String regex = "&\\D+?;";
System.out.println (str.replaceAll(regex,"Z"));
This should work
Match the initial &, then all characters that are not the tailing ;, then that tailing ; like so: &[^;]+; If not matching numbers (as suggested by your example with \D) is a requirement, add the numbers to the negated character set: [^;0-9] To make it replace all occurrences, add the global flag g. The site regexr.com is a handy tool to create regexes.
Edit: Sorry, I initially read your question wrong.
This question already has answers here:
How to Split a mathematical expression on operators as delimiters, while keeping them in the result?
(5 answers)
Closed 4 years ago.
I want to split a mathematical function by the sign of the variables in it like this :
input--> x-5y+3z=10
output--> [x,-5y,+3z,=10]
this code does not work in the way i want :
String function = "x-5y+3z=10";
String split = function.split("=|-|\\+");
the output of the array is :
[x,5y,3z,10]
so what is the correct regex for this ?
The "problem" using split is that the delimiter used will be removed, because it'll takt the parts that are between this delimiter, you need a pattern that is non-capturing or with a simple lookahead : match something wich is before something else
The pattern (?=[-+=]) would work, it'll take the part that starts with a -+= symbol without removing it :
String function = "x-5y+3z=10";
String[] split = function.split("(?=[-+=])");
System.out.println(Arrays.toString(split)); //[x, -5y, +3z, =10]
Some doc on Lookahead
This question already has answers here:
How to use beginning and endline markers in regex for Java String?
(5 answers)
How to use java regex to match a line
(2 answers)
Closed 5 years ago.
given the following expression:
Pattern.compile("^Test.*\n").matcher("Test 123\nNothing\nTest 2\n").replaceAll("foo\n")
This yields:
"foo\nNothing\nTest 2\n"
for me. I expected that the last line is also replaced to foo\n since there is a linebreak immediately before Test 2 in the input string.
Why is doesn't the regex match there?
You have to add the multiline flag to the pattern: Pattern.MULTILINE.
Pattern.compile("^Test.*\n", Pattern.MULTILINE).matcher("Test 123\nNothing\nTest 2\n").replaceAll("foo\n")
By Default the match is only single line. For more Informations see the javadoc
At the beginning of your regex you have a ^ sign which normally anchors the regex to the beginning of a tested string. You need to specify multiline regex option (Oracle Documentation link) to make it apply to start of each line instead.
Try this (I have split the lines for legibility, feel free to oneline it back):
Pattern.compile("^Test.*\n", Pattern.MULTILINE)
.matcher("Test 123\nNothing\nTest 2\n")
.replaceAll("foo\n")
Unfortunately I do not have Java environment set up at the moment, so I'm unable to check this by myself.
This question already has answers here:
RegEx for matching UK Postcodes
(33 answers)
Closed 7 years ago.
I'm trying to create a regex matching the following patterns (with and without space):
M1 1AA, M60 1NW, CR2 6XH, DN55 1PT, W1A 1HQ and EC1A 1BB
I'm very new at this and find it hard to create a functional regex for all the examples above.
Searching here and there I found a regex that might work for some of the patterns but I don't know how to add the condition "with or without space" for each type of postcode.
Here the regex I found on another post "^(A-PR-UWYZ [0-9][ABD-HJLNP-UW-Z]{2})"
How do I add the space/no space condition? In order to match M11AA or M1 1AA.
You need this regex:
^([A-PR-UWYZ](([0-9](([0-9]|[A-HJKSTUW])?)?)|([A-HK-Y][0-9]([0-9]|[ABEHMNPRVWXY])?)) ?[0-9][ABD-HJLNP-UW-Z]{2})$
^
This space must be set as optional with ? quantifier that means 0 or 1 repetition.
See demo
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
I m trying regex to get the strings
starting with # and
not ending with a dot(.)
For that i tried the java code(link here) but this does not show any results -
#(\\w+)*(?<!.(.))*$
The string i m trying is -
This is a test\nAnother #pradyut#test ht#html.com\ntest\n#art\n#cool#paintings#collections
This should return
pradyut
test
cool
The result html ending with a .com should not return.
Regards
You can use this regex:
(?<=#)\w+\b(?!\.)
In Java you have to use:
(?<=#)\\w+\\b(?!\\.)
Regex Demo