Matching a string to a regex from html input - java

I'm having a little trouble figuring out what to do.
Basically using java I'm trying to:
Reading in the html from a website
I want to find the content after a certain string in this case being
title="
Store that in a string.
The first and last steps are simple for me but I'm having no luck (and never had with regex).
I believe this is the beginning of what I need:
String regex = "(?<=title=\")\\S+";
Pattern name = Pattern.compile(regex);
After that I have no clue. Any help?

import java.util.regex.Matcher;
import java.util.regex.Pattern;
String EXAMPLE_TEST = "......";
Pattern pattern = Pattern.compile("(?<=title=\")(\\S+)")
Matcher matcher = pattern.matcher(EXAMPLE_TEST);
while (matcher.find()) {
System.out.println(matcher.group());
}
Note: You might consider to use regex pattern (?<=title=\")([^\"]*)

List<String> result_list = new ArrayList<String>();
Pattern p = Pattern.compile("title=\"(.*)\"");
Matcher m = p.matcher("title=\"test\"");
boolean result = m.find();
while(result)
{
result_list.add(m.group(0));
result = m.find();
}

Related

Java replaceAll but the specified regex

Can't get my head around this for quite some time already. I have this piece of code:
getStringFromDom(doc).replaceAll("contract=\"\\d*\"|name=\"\\p{L}*\"", "");
Basically I need it to work literally the opposite way - to replace everything BUT the specified regex. I've been trying to do it with the negative lookahead to no avail.
For your particular task, I think
getStringFromDom(doc).replaceAll(".*?(contract=\"\\d*\"|name=\"\\p{L}*\").*", "$1");
should do what you need.
You want to remove everything that does not match the pattern. This is the same as simply filtering the pattern matches. Use the regex to find matches for that pattern, then collect the matches in a stringbuilder.
Matcher m = Pattern.compile(your pattern).matcher(your input);
StringBuilder sb = new StringBuilder();
while (m.find()) sb.append (m.group()).append('\n');
String result = sb.toString();
I also think that removing what your are not looking for is a double negative. Concentrate on what you are looking for and use a pattern matching for that. This example searches your document for any name attributes:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String input = "<AnotherDoc accNum=\"1111\" docDate=\"2017-09-26\" docNum=\"2222\" name=\"foo\"> <anotherTag>some date</anotherTag>";
Pattern pattern = Pattern.compile("name=\"[^\\\"]*\""); // value are all characters but "
Matcher matcher = pattern.matcher(input);
while (matcher.find())
System.out.println(matcher.group());
}
}
This prints:
name="foo"

Extract String from a within a String using a Regular Expression

I have a very large String containing within it some markers like:
{codecitation class="brush: java; gutter: true;" width="700px"}
I'd need to collect all the markers contained in the long String. The difficulty I find in this task is that the markers all contain different parameter values. The only thing they have in common is the initial part that is:
{codecitation class="brush: [VARIABLE PART] }
Do you have any suggestion to collect all the markers in Java using a Regular Expression ?
Use pattern matching to find the markers as below. I hope this will help.
String xmlString = "{codecitation class=\"brush: java; gutter: true;\" width=\"700px\"}efasf{codecitation class=\"brush: java; gutter: true;\" width=\"700px\"}";
Pattern pattern = Pattern.compile("(\\{codecitation)([0-9 a-z A-Z \":;=]{0,})(\\})");
Matcher matcher = pattern.matcher(xmlString);
while (matcher.find()) {
System.out.println(matcher.group());
}
I guess you are particularly interested in the brush: java; and gutter: true; parts.
Maybe this snippet helps:
package test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CodecitationParserTest {
public static void main(String[] args) {
String testString = "{codecitation class=\"brush: java; gutter: true;\" width=\"700px\"}";
Pattern codecitationPattern = Pattern
.compile("\\{codecitation class=[\"]([^\"]*)[\"][^}]*\\}");
Matcher matcher = codecitationPattern.matcher(testString);
Pattern attributePattern = Pattern
.compile("\\s*([^:]*): ([^;]*);(.*)$");
Matcher attributeMatcher;
while (matcher.find()) {
System.out.println(matcher.group(1));
attributeMatcher = attributePattern.matcher(matcher.group(1));
while (attributeMatcher.find()) {
System.out.println(attributeMatcher.group(1) + "->"
+ attributeMatcher.group(2));
attributeMatcher = attributePattern.matcher(attributeMatcher
.group(3));
}
}
}
}
The codecitationPattern extracts the content of the class attribute of a codecitation element. The attributePattern extracts the first key and value and the rest, so you can apply it recursively.

Regex parse string in java

I am using Java. I need to parse the following line using regex :
<actions>::=<action><action>|X|<game>|alpha
It should give me tokens <action>, <action>,X and <game>
What kind of regex will work?
I was trying sth like: "<[a-zA-Z]>" but that doesn't take care of X or alpha.
You can try something like this:
String str="<actions>::=<action><action>|X|<game>|alpha";
str=str.split("=")[1];
Pattern pattern = Pattern.compile("<.*?>|\\|.*?\\|");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
You should have something like this:
String input = "<actions>::=<action><action>|X|<game>|alpha";
Matcher matcher = Pattern.compile("(<[^>]+>)(<[^>]+>)\\|([^|]+)\\|(<[^|]+>)").matcher(input);
while (matcher.find()) {
System.out.println(matcher.group().replaceAll("\\|", ""));
}
You didn't specefied if you want to return alpha or not, in this case, it doesn't return it.
You can return alpha by adding |\\w* to the end of the regex I wrote.
This will return:
<action><action>X<game>
From the original pattern it is not clear if you mean that literally there are <> in the pattern or not, i'll go with that assumption.
String pattern="<actions>::=<(.*?)><(.+?)>\|(.+)\|<(.*?)\|alpha";
For the java code you can use Pattern and Matcher: here is the basic idea:
Pattern p = Pattern.compile(pattern, Pattern.DOTALL|Pattern.MULTILINE);
Matcher m = p.matcher(text);
m.find();
for (int g = 1; g <= m.groupCount(); g++) {
// use your four groups here..
}
You can use following Java regex:
Pattern pattern = Pattern.compile
("::=(<[^>]+>)(<[^>]+>)\\|([^|]+)\\|(<[^>]+>)\\|(\\w+)$");

Remove part of String following regex match in Java

I want to remove a part of a string following what matches my regex.
I am trying to make a TV show organization program and I want to cut off anything in the name following the season and episode marker in the form SXXEXX where X is a digit.
I grasped the regex model fairly easily to create "[Ss]\d\d[Ee]\d\d" which should match properly.
I want to use the Matcher method end() to get the last index in the string of the match but it does not seem to be working as I think it should.
Pattern p = Pattern.compile("[Ss]\\d\\d[Ee]\\d\\d");
Matcher m = p.matcher(name);
if(m.matches())
return name.substring(0, m.end());
If someone could tell me why this doesn't work and suggest a proper way to do it, that would be great. Thanks.
matches() tries to match the whole string again the pattern. If you want to find your pattern within a string, use find(), find() will search for the next match in the string.
Your code could be quite the same:
if(m.find())
return name.substring(0, m.end());
matches matches the entire string, try find()
You could capture the name as well:
String name = "a movie S01E02 with some stuff";
Pattern p = Pattern.compile("(.*[Ss]\\d\\d[Ee]\\d\\d)");
Matcher m = p.matcher(name);
if (m.find())
System.out.println(m.group());
else
System.out.println("No match");
Will capture and print:
a movie S01E02
This should work
.*[Ss]\d\d[Ee]\d\d
In java (I'm rusty) this will be
String ResultString = null;
Pattern regex = Pattern.compile(".*[Ss]\\d\\d[Ee]\\d\\d");
Matcher regexMatcher = regex.matcher("Title S11E11Blah");
if (regexMatcher.find()) {
ResultString = regexMatcher.group();
}
Hope this helps

Java regex to extract text between tags

I have a file with some custom tags and I'd like to write a regular expression to extract the string between the tags. For example if my tag is:
[customtag]String I want to extract[/customtag]
How would I write a regular expression to extract only the string between the tags. This code seems like a step in the right direction:
Pattern p = Pattern.compile("[customtag](.+?)[/customtag]");
Matcher m = p.matcher("[customtag]String I want to extract[/customtag]");
Not sure what to do next. Any ideas? Thanks.
You're on the right track. Now you just need to extract the desired group, as follows:
final Pattern pattern = Pattern.compile("<tag>(.+?)</tag>", Pattern.DOTALL);
final Matcher matcher = pattern.matcher("<tag>String I want to extract</tag>");
matcher.find();
System.out.println(matcher.group(1)); // Prints String I want to extract
If you want to extract multiple hits, try this:
public static void main(String[] args) {
final String str = "<tag>apple</tag><b>hello</b><tag>orange</tag><tag>pear</tag>";
System.out.println(Arrays.toString(getTagValues(str).toArray())); // Prints [apple, orange, pear]
}
private static final Pattern TAG_REGEX = Pattern.compile("<tag>(.+?)</tag>", Pattern.DOTALL);
private static List<String> getTagValues(final String str) {
final List<String> tagValues = new ArrayList<String>();
final Matcher matcher = TAG_REGEX.matcher(str);
while (matcher.find()) {
tagValues.add(matcher.group(1));
}
return tagValues;
}
However, I agree that regular expressions are not the best answer here. I'd use XPath to find elements I'm interested in. See The Java XPath API for more info.
To be quite honest, regular expressions are not the best idea for this type of parsing. The regular expression you posted will probably work great for simple cases, but if things get more complex you are going to have huge problems (same reason why you cant reliably parse HTML with regular expressions). I know you probably don't want to hear this, I know I didn't when I asked the same type of questions, but string parsing became WAY more reliable for me after I stopped trying to use regular expressions for everything.
jTopas is an AWESOME tokenizer that makes it quite easy to write parsers by hand (I STRONGLY suggest jtopas over the standard java scanner/etc.. libraries). If you want to see jtopas in action, here are some parsers I wrote using jTopas to parse this type of file
If you are parsing XML files, you should be using an xml parser library. Dont do it youself unless you are just doing it for fun, there are plently of proven options out there
A generic,simpler and a bit primitive approach to find tag, attribute and value
Pattern pattern = Pattern.compile("<(\\w+)( +.+)*>((.*))</\\1>");
System.out.println(pattern.matcher("<asd> TEST</asd>").find());
System.out.println(pattern.matcher("<asd TEST</asd>").find());
System.out.println(pattern.matcher("<asd attr='3'> TEST</asd>").find());
System.out.println(pattern.matcher("<asd> <x>TEST<x>asd>").find());
System.out.println("-------");
Matcher matcher = pattern.matcher("<as x> TEST</as>");
if (matcher.find()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
System.out.println(i + ":" + matcher.group(i));
}
}
String s = "<B><G>Test</G></B><C>Test1</C>";
String pattern ="\\<(.+)\\>([^\\<\\>]+)\\<\\/\\1\\>";
int count = 0;
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(s);
while(m.find())
{
System.out.println(m.group(2));
count++;
}
Try this:
Pattern p = Pattern.compile(?<=\\<(any_tag)\\>)(\\s*.*\\s*)(?=\\<\\/(any_tag)\\>);
Matcher m = p.matcher(anyString);
For example:
String str = "<TR> <TD>1Q Ene</TD> <TD>3.08%</TD> </TR>";
Pattern p = Pattern.compile("(?<=\\<TD\\>)(\\s*.*\\s*)(?=\\<\\/TD\\>)");
Matcher m = p.matcher(str);
while(m.find()){
Log.e("Regex"," Regex result: " + m.group())
}
Output:
10 Ene
3.08%
final Pattern pattern = Pattern.compile("tag\\](.+?)\\[/tag");
final Matcher matcher = pattern.matcher("[tag]String I want to extract[/tag]");
matcher.find();
System.out.println(matcher.group(1));
I prefix this reply with "you shouldn't use a regular expression to parse XML -- it's only going to result in edge cases that don't work right, and a forever-increasing-in-complexity regex while you try to fix it."
That being said, you need to proceed by matching the string and grabbing the group you want:
if (m.matches())
{
String result = m.group(1);
// do something with result
}
This works for me, use in your main method below Scanner input. Works for Hackerrank "Tag Content Extractor" also.
boolean matchFound = false;
Pattern r = Pattern.compile("<(.+)>([^<]+)</\\1>");
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println(m.group(2));
matchFound = true;
}
if ( ! matchFound) {
System.out.println("None");
}
testCases--;

Categories