Skip to content

Tag: regex

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 wh…

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 o…

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…

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…