I have a very simple regex question. Suppose I have 2 conditions: url =http://www.abc.com/cde/def url =https://www.abc.com/sadfl/dsaf How can I extract the baseUrl using regex? Sample output: http://www.abc.com https://www.abc.com Answer Like this: However, you should use the URI class instead, like this:
Tag: regex
Using Regular Expressions
I am having problems trying to use the regular expression that I used in JavaScript. On a web page, you may have: Renewal Date: 03 May 2010
I just want to be able to …
Is using “\\” to match ” with Regex in Java the most Readable Way?
I know that the following works but it is not that readable, is there any way to make it more readable in the code itself without the addition of a comment? //Start her off String sampleregex = “\\”…
Java: splitting a comma-separated string but ignoring commas in quotes
I have a string vaguely like this: that I want to split by commas — but I need to ignore commas in quotes. How can I do this? Seems like a regexp approach fails; I suppose I can manually scan and enter a different mode when I see a quote, but it would be nice to use preexisting libraries. (edit:
What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?
The title pretty much says it all. What’s the simplest/most elegant way that I can convert, in Java, a string from the format “THIS_IS_AN_EXAMPLE_STRING” to the format “ThisIsAnExampleString”? I figure there must be at least one way to do it using String.replaceAll() and a regex.…
How to escape a square bracket for Pattern compilation?
I have comma separated list of regular expressions: I have done a split on the comma. Now I’m trying to match this regex against a generated password. The problem is that Pattern.compile does not like square brackets that is not escaped. Can some please give me a simple function that takes a string like…
Regex for tree structures?
Are there regular expression equivalents for searching and modifying tree structures? Concise mini-languages (like perl regex) are what I am looking for. Here is an example that might clarify what I am looking for. An operation that would be possible on the above tree is “move subtree at node 2.1 into t…
Remove HTML tags from a String
Is there a good way to remove HTML from a Java string? A simple regex like will work, but some things like & won’t be converted correctly and non-HTML between the two angle brackets will be removed (i.e. the .*? in the regex will disappear). Answer Use a HTML parser instead of regex. This is dea…
Regular expression to match URLs in Java
I use RegexBuddy while working with regular expressions. From its library I copied the regular expression to match URLs. I tested successfully within RegexBuddy. However, when I copied it as Java String flavor and pasted it into Java code, it does not work. The following class prints false: Does anyone know w…