i wanna extract a part of url which is at the middle of it, by using regex in java
this is what i tried,mostly the problem to detect java+regexis that its in the middle of last part of url and i have no idea how to ignore the characters after it, my regex just ignoring before it:
String regex = "https://www\\.google\\.com/(search)?q=([^/]+)/";
String url = "https://www.google.com/search?q=regex+java&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a";
Pattern pattern = Pattern.compile (regex);
Matcher matcher = pattern.matcher (url);
if (matcher.matches ())
{
int n = matcher.groupCount ();
for (int i = 0; i <= n; ++i)
System.out.println (matcher.group (i));
}
}
the result should be regex+java or even regex java . but my code didnt work out...
Try:
String regex = "https://www\\.google\\.com/search\\?q=([^&]+).*";
String url = "https://www.google.com/search?q=regex+java&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a";
Pattern pattern = Pattern.compile (regex);
Matcher matcher = pattern.matcher (url);
if (matcher.matches ())
{
int n = matcher.groupCount ();
for (int i = 0; i <= n; ++i)
System.out.println (matcher.group (i));
}
The result is:
https://www.google.com/search?q=regex+java&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
regex+java
EDIT
Replacing all pluses before printing:
for (int i = 0; i <= n; ++i) {
String str = matcher.group (i).replaceAll("\\+", " ");
System.out.println (str);
}
String regex = "https://www\\.google\\.com/?(search)\\?q=([^&]+)?";
String url = "https://www.google.com/search?q=regex+java&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(url);
while (matcher.find()) {
System.out.println(matcher.group());
}
This should do your job.
Related
I'm using regex to find a pattern
I need to find all matches in this way :
input :"word1_word2_word3_..."
result: "word1_word2","word2_word3", "word4_word5" ..
It can be done using (?=) positive lookahead.
Regex: (?=(?:_|^)([^_]+_[^_]+))
Java code:
String text = "word1_word2_word3_word4_word5_word6_word7";
String regex = "(?=(?:_|^)([^_]+_[^_]+))";
Matcher matcher = Pattern.compile(regex).matcher(text);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Output:
word1_word2
word2_word3
word3_word4
...
Code demo
You can do it without regex, using split:
String input = "word1_word2_word3_word4";
String[] words = input.split("_");
List<String> outputs = new LinkedList<>();
for (int i = 0; i < words.length - 1; i++) {
String first = words[i];
String second = words[i + 1];
outputs.add(first + "_" + second);
}
for (String output : outputs) {
System.out.println(output);
}
Hi I would like to store my matches in my array however constantly getting errors of nullpointer or out of bounds.
final String mcontentURI[] = new String[count];
for (int i = 0; i < count; i++) {
Pattern p = Pattern.compile("src=\"(.*?)\"");
Matcher m = p.matcher(content_val);
if (m.find()) {
mcontentURI[i] = (m.group(i+1));
}
}
Since you keep re-compiling the same regex, the group number is going to stay the same. You can put it at different indexes of the array, though:
final String mcontentURI[] = new String[count];
final Pattern p = Pattern.compile("src=\"(.*?)\"");
for (int i = 0; i < count; i++) {
Matcher m = p.matcher(content_val); // Use different strings here
if (m.find()) {
mcontentURI[i] = m.group(1);
}
}
Note that mcontentURI[i] would remain null for indexes for which the pattern did not match.
If you want to search the same string, do this:
final String mcontentURI[] = new String[count];
final Pattern p = Pattern.compile("src=\"(.*?)\"");
Matcher m = p.matcher(content_val);
int i = 0;
while (i < count && m.find()) {
mcontentURI[i++] = m.group(1);
}
How can I split a string by 2 characters with shifting.
For example;
My string is = todayiscold
My target is: "to","od","da","ay","yi","is","sc","co","ol","ld"
but with this code:
Arrays.toString("todayiscold".split("(?<=\\G.{2})")));
I get: `"to","da","yi","co","ld"
anybody helps?
Try this:
String e = "example";
for (int i = 0; i < e.length() - 1; i++) {
System.out.println(e.substring(i, i+2));
}
Use a loop:
String test = "abcdefgh";
List<String> list = new ArrayList<String>();
for(int i = 0; i < test.length() - 1; i++)
{
list.add(test.substring(i, i + 2));
}
Following regex based code should work:
String str = "todayiscold";
Pattern p = Pattern.compile("(?<=\\G..)");
Matcher m = p.matcher(str);
int start = 0;
List<String> matches = new ArrayList<String>();
while (m.find(start)) {
matches.add(str.substring(m.end()-2, m.end()));
start = m.end()-1;
}
System.out.println("Matches => " + matches);
Trick is to use end()-1 from last match in the find() method.
Output:
Matches => [to, od, da, ay, yi, is, sc, co, ol, ld]
You cant use split in this case because all split does is find place to split and brake your string in this place, so you cant make same character appear in two parts.
Instead you can use Pattern/Matcher mechanisms like
String test = "todayiscold";
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile("(?=(..))");
Matcher m = p.matcher(test);
while(m.find())
list.add(m.group(1));
or even better iterate over your Atring characters and create substrings like in D-Rock's answer
I have a string as follows:
"This is #awesome #dude"
From this string i want to extract awesome and dude and create a string
output==> "awesome,dude"
So my code is like following:
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(textStr);
while (matcher.find()){
mergedStr += matcher.group() +",";
}
But this creates an artifact in the end
output==> "awesome,dude," //<-- egghh comma.. in the end
What is a better way to solve this out.
Another approach:
boolean needComma = false;
while (blah, blah, blah) {
if (needComma) {
string += ",";
}
string += word;
needComma = true;
}
But there are a dozen different approaches.
This is one option:
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(textStr);
while (matcher.find()){
if (!mergedStr.isEmpty())
mergedStr += ",";
mergedStr += matcher.group();
}
Here is another common approach:
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(textStr);
StringBuilder sb = new StringBuilder();
while (matcher.find()){
sb.append(matcher.group()).append(",");
}
return sb.toString().replaceAll(",$", "");
If you don't want to use a regex, you could do it like this:
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(textStr);
StringBuilder sb = new StringBuilder();
while (matcher.find()){
sb.append(matcher.group()).append(",");
}
if (sb.length() == 0) {
return "";
}
else {
return sb.toString().substring(0, sb.length() - 1);
}
A useful pattern that I often use for this kind of thing is to append the first item, and then append the remainder of the items preceded by the separator. This avoids unnecessary conditionals in loops or postprocessing to remove trailing separators.
I know, microoptimizations blah, blah, sixth circle of hell, blah, blah, but just including here for your amusement:
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(textStr);
StringBuilder mergedStr = new StringBuilder();
if (matcher.find()) {
mergedStr.append(matcher.group());
while (matcher.find()) {
mergedStr.append(',').append(matcher.group());
}
}
return mergedStr.toString();
Also, I'm not 100% convinced that replacing a quadratic algorithm (string concatenation) with a linear algorithm (StringBuilder) qualifies as a microoptimization in the bad sense.
String input = "#awesome#dude";
List<String> strSplit = new ArrayList<String>();
String result = "";
Matcher matcher = Pattern.compile("(?<=#)\\w+").matcher(input);
while (matcher.find()){
strSplit.add(matcher.group());
}
for(int j = 0; j< strSplit.size(); j++){
result = result + strSplit.get(j);
if(j < strSplit.size() -1){
result = result+",";
}
}
System.out.println("Result : " + result);
How to read the string from the position (Example)5 to the end of the string in java.
QRegExp StripType(re, Qt::CaseInsensitive);
int p = StripType.indexIn(line, 0);
int len = StripType.matchedLength();
String tmp += line.mid(len);
How to convert QT into java
Where re is in the above code is regular expression and i want to covert the above into java i have tried
String s =pattern.toString();
int pos = s.indexOf(line);
Matcher matcher = Pattern.compile(re).matcher(line);
if (matcher.find()) {
System.out.println(matcher.group());
} else {
System.out.println("String contains no character other than that");
}
len = matcher.start();
But its not working correct
Thanks in Advance
To begin with you should add the Pattern.CASE_INSENSITIVE flag.
Matcher matcher = Pattern.compile(re, Pattern.CASE_INSENSITIVE).matcher(line);