Skip to content
Advertisement

Tag: regex

Difference in results between Java matches vs JavaScript match

I was brushing up on my regular expressions in java when I did a simple test But in JavaScript What is going on here? And can I make my java regex pattern “q” behave the same as JavaScript? Answer In JavaScript match returns substrings which matches used regex. In Java matches checks if entire string matches regex. If you want

Delete anything between “<” and “>” in a String

I have try restString = restString.replaceAll(“\&lt;.*\&gt;”, “”); and restString = restString.replaceAll(“\&lt;[^(\&gt)]*\&gt;”, “”);. Both seems don’t work. I don’t know if I could represent the meaning in the regular expression. Answer Make your regex non-greedy: Also I used (?s) to make dot match newlines as well.

Check and extract a number from a String in Java

I’m writing a program where the user enters a String in the following format: I need to check that there is a number in the String and then extract just the number. If i use .contains(“\d+”) or .contains(“[0-9]+”), the program can’t find a number in the String, no matter what the input is, but .matches(“\d+”)will only work when there is

Set two flags in Java regex.Pattern

I need a matcher like this: and the problem is that it is not simple ASCII. I know that in this particular case I could use [u00FCu00DC] for the ü, but I need to be a bit more general (building the regex from other matcher groups). So according to javadocs: By default, case-insensitive matching assumes that only characters in the

Java regex capturing groups indexes

I have the following line, I need to fetch the word ABC, I wrote the following code snippet, So if I put group(0) I get ABC: but if I put group(1) it is ABC, so I want to know What does this 0 and 1 mean? It will be better if anyone can explain me with good examples. The regex

Regex to extract valid Http or Https

I’m currently having some issues with a regex to extract a URL. I want my regex to take URLS such as: Through some tutorials, I’ve learned that this regex will find all the above: ^(http|https)://.*$ however, it will also take http://local:1000;http://invalid http://khttp://as a single string when it shouldn’t take it at all. I understand that my expression isn’t written to

Java Regular Expression split keeping contractions

When using split(), what regular expression would allow me to keep all word characters but would also preserve contractions like don’t won’t. Anything with word characters on both sides of the apostrophe but removes any leading or trailing apostraphes such as ’tis or dogs’. I have: but it keeps the leading and trailing punctuation. Input of ‘Tis the season, for

Advertisement