Getting multiple matches via regex - java

I want to retrieve a strings from a global string via Matcher & Pattern using REGEX.
String str = "<strong>ABC</strong>123<strong>DEF</strong>"
Pattern pattern = Pattern.compile("<strong>(.*)</strong>");
Matcher matcher = pattern.matcher(str);
My problem is that the matcher gives me just one match that is inside the global tag strong:
ABC</strong>123<strong>DEF
My objective is to get 2 matches:
ABC
DEF
Thank you very match for you help.

You need a non greedy regex:
Pattern pattern = Pattern.compile("<strong>.*?</strong>");
Use ? to specify non greedy. This means it will match the first match it finds instead of the outer most match...
If you only want ABC and DEF then you can do something like this using lookaheads and lookbehinds:
String str = "<strong>ABC</strong>123<strong>DEF</strong>";
Pattern pattern = Pattern.compile("((?<=<strong>).*?(?=</strong>))");
Matcher matcher = pattern.matcher(str);
while(matcher.find())
{
System.out.println(matcher.group());
}
If you do a google search you should be able to find information on lookaheads and lookbehinds...

I recommend to use JSOUP to parse your HTML code instead of regex as
Document doc = Jsoup.parse("<strong>ABC</strong>123<strong>DEF</strong>");
// select your tag
Elements elements = doc.select("strong");
// get the iterator to traverse all elements
Iterator<Element> it = elements.iterator();
// loop through all elements and fetch their text
while (it.hasNext()) {
System.out.println(it.next().text());
}
Output :
ABC
DEF
or get Output as single string
Document doc = Jsoup.parse("<strong>ABC</strong>123<strong>DEF</strong>");
Elements elements = doc.select("strong");
System.out.println(elements.text());
Output:
ABC DEF
Download Jsoup and add it as a dependency

Related

Java Regex : Extract a specific pattern from a string "I_INSERT_TO_TOPIC_345674_123456_4.json"

I want to extract only "_123456_4" from this string using java Regex.
I_INSERT_TO_TOPIC_345674_123456_4.json
I have tried
Pattern.compile("(_([^_]*_[^_]))") and Pattern.compile("_" + "([^[0-9]]*)" + "_[0-9]") but these do not work.
If you want to get 2 group of digits just before .json then you can use regex group to find the required match. You can modify the pattern as per your requirement.
Pattern p = Pattern.compile("(_\\d+_\\d+)\\.json");
Matcher matcher = p.matcher(s);
if (matcher.find()) {
String group = matcher.group(1);
}
【\_[0-9]\*\_[0-9]\*(?=\\.)】
You can try to see if this works

extract a set of a characters between some characters

I have a string email = John.Mcgee.r2d2#hitachi.com
How can I write a java code using regex to bring just the r2d2?
I used this but got an error on eclipse
String email = John.Mcgee.r2d2#hitachi.com
Pattern pattern = Pattern.compile(".(.*)\#");
Matcher matcher = patter.matcher
for (Strimatcher.find()){
System.out.println(matcher.group(1));
}
To match after the last dot in a potential sequence of multiple dots request that the sequence that you capture does not contain a dot:
(?<=[.])([^.]*)(?=#)
(?<=[.]) means "preceded by a single dot"
(?=#) means "followed by # sign"
Note that since dot . is a metacharacter, it needs to be escaped either with \ (doubled for Java string literal) or with square brackets around it.
Demo.
Not sure if your posting the right code. I'll rewrite it based on what it should look like though:
String email = John.Mcgee.r2d2#hitachi.com
Pattern pattern = Pattern.compile(".(.*)\#");
Matcher matcher = pattern.matcher(email);
int count = 0;
while(matcher.find()) {
count++;
System.out.println(matcher.group(count));
}
but I think you just want something like this:
String email = John.Mcgee.r2d2#hitachi.com
Pattern pattern = Pattern.compile(".(.*)\#");
Matcher matcher = pattern.matcher(email);
if(matcher.find()){
System.out.println(matcher.group(1));
}
No need to Pattern you just need replaceAll with this regex .*\.([^\.]+)#.* which mean get the group ([^\.]+) (match one or more character except a dot) which is between dot \. and #
email = email.replaceAll(".*\\.([^\\.]+)#.*", "$1");
Output
r2d2
regex demo
If you want to go with Pattern then you have to use this regex \\.([^\\.]+)# :
String email = "John.Mcgee.r2d2#hitachi.com";
Pattern pattern = Pattern.compile("\\.([^\\.]+)#");
Matcher matcher = pattern.matcher(email);
if (matcher.find()) {
System.out.println(matcher.group(1));// Output : r2d2
}
Another solution you can use split :
String[] split = email.replaceAll("#.*", "").split("\\.");
email = split[split.length - 1];// Output : r2d2
Note :
Strings in java should be between double quotes "John.Mcgee.r2d2#hitachi.com"
You don't need to escape # in Java, but you have to escape the dot with double slash \\.
There are no syntax for a for loop like you do for (Strimatcher.find()){, maybe you mean while

Which regular expression or pattern should be used to grab following contents from the text file?

I need to split text starting from "9=DEXTER" upto the next "9=DEXTER". How to write pattern regex for it in java. using end of the line doesnt works since some phrase merged with the previouse phrase line.
9=DEXTER.1.19=14161035=X49=PARFX56=c3_user134=847152=20130408-22:27:59.606262=aquru2D00000016268=1618279=2269=055=USD/JPY270=27426.89271=01023=35336=3346=0273=22:27:22.935279=0269=055=USD/JPY270=27386.32271=460000001023=489336=3346=1273=22:27:22.9358722=0279=2269=055=USD/JPY270=27388.25271=01023=472336=3346=0273=22:27:22.935279=0269=055=USD/JPY270=27429.08271=440000001023=7336=3346=1273=22:27:22.9358722=0279=0269=055=USD/JPY270=27410.08271=440000001023=233336=3346=1273=22:27:22.9368722=0279=2269=055=USD/JPY270=27375.1271=01023=635336=3346=0273=22:27:23.013279=0269=0279=2269=055=USD/JPY270=27429.01271=01023=9336=3346=0273=22:27:23.015279=0269=055=USD/JPY270=27377.79271=410000001023=599336=3346=1273=22:27:23.0158722=0279=2269=055=USD/JPY270=27372.1271=01023=679336=3346=0273=22:27:23.019279=0269=055=USD/JPY270=27363.78271=480000001023=799336=3346=1273=22:27:23.0198722=0279=2269=055=USD/JPY270=27403.94271=01023=299336=3346=0273=22:27:23.019279=0269=055=USD/JPY270=27418.77271=440000001023=119336=3346=1273=22:27:23.0198722=0279=2269=055=USD/JPY270=27379.15271=01023=587336=3346=0273=22:27:23.019279=0269=055=USD/JPY270=27396.59271=410000001023=377336=3346=1273=22:27:23.0198722=0279=0269=055=USD/JPY270=27415.88271=470000001023=153336=3346=1273=22:27:23.0198722=0279=1269=055=USD/JPY270=27376.14271=460000001023=625336=3346=1273=22:27:23.0198722=0279=0269=055=USD/JPY270=27410.47271=950000001023=234336=3346=2273=22:27:23.0198722=0279=2269=055=USD/JPY270=27402.08271=01023=323336=3346=0273=22:27:23.019279=1269=055=USD/JPY270=27366.49271=880000001023=762336=3346=2273=22:27:23.0198722=0279=2269=055=USD/JPY270=27374.05271=01023=658336=3346=0273=22:27:23.019279=1269=055=USD/JPY270=27411.14271=850000001023=225336=3346=2273=22:27:23.0198722=0279=2269=055=USD/JPY270=27396.3271=01023=384336=3346=0273=22:27:23.019279=0269=055=USD/JPY270=27367.61271=410000001023=743336=3346=1273=22:27:23.0198722=0279=2269=055=USD/JPY270=27376.69271=01023=618336=3346=0273=22:27:23.019279=0269=055=USD/JPY270=27384.88271=430000001023=514336=3346=1273=22:27:23.0198722=0279=2269=055=USD/JPY270=27409.38271=01023=245336=3346=0273=22:27:23.019279=0269=055=USD/JPY270=27425.95271=460000001023=45336=3346=1273=22:27:23.0198722=0279=2269=055=USD/JPY270=27399.42271=01023=350336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27365.76271=470000001023=769336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27425.19271=01023=56336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27409.56271=480000001023=244336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27414.01271=01023=180336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27369.37271=440000001023=716336=3346=1273=22:27:23.0208722=0279=1269=055=USD/JPY270=27391.09271=480000001023=436336=3346=1273=22:27:23.0208722=0279=0269=055=USD/JPY270=27408.21271=410000001023=256336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27401.79271=01023=325336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27365.15271=420000001023=780336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27422.14271=01023=79336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27385.93271=400000001023=497336=3346=1273=22:27:23.0208722=0279=0269=055=USD/JPY270=27372.05271=460000001023=676336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27389.2271=01023=458336=3346=0273=22:27:23.020279=2269=055=USD/JPY270=27363.57271=01023=803336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27395.22271=440000001023=392336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27414.19271=01023=175336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27369.27271=440000001023=719336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27363.87271=01023=798336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27376.07271=470000001023=622336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27367.43271=01023=749336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27375.68271=440000001023=630336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27394.26271=01023=400336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27399.66271=450000001023=344336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27391.84271=01023=428336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27367.69271=430000001023=744336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27383.3271=01023=524336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27391.39271=470000001023=434336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27376.04271=01023=623336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27422.87271=440000001023=74336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27394.4271=01023=400336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27406.86271=400000001023=272336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27371.41271=01023=686336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27391.21271=420000001023=436336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27393.08271=01023=419336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27411.57271=440000001023=217336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27377.01271=01023=613336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27417.43271=460000001023=137336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27375.62271=01023=632336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27406.25271=480000001023=280336=3346=1273=22:27:23.0208722=0279=0269=055=USD/JPY270=27393.35271=450000001023=420336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27369.79271=01023=712336=3346=0273=22:27:23.020279=0269=055=USD/JPY270=27367.1271=410000001023=754336=3346=1273=22:27:23.0208722=0279=2269=055=USD/JPY270=27428.03271=01023=22336=3346=0273=22:27:23.021279=0269=055=USD/JPY270=27364.26271=470000001023=791336=3346=1273=22:27:23.0218722=0279=2269=055=USD/JPY270=27410.59271=01023=231336=3346=0273=22:27:23.021279=0269=055=USD/JPY270=27409.68271=410000001023=242336=3346=1273=22:27:23.0218722=0279=2269=055=USD/JPY270=27368.55271=01023=732336=3346=0273=22:27:23.021279=0269=055=USD/JPY270=27361.51271=470000001023=826336=3346=1273=22:27:23.0218722=0279=2269=055=USD/JPY270=27427.01271=01023=34336=3346=0273=22:27:23.021279=0269=055=USD/JPY270=27373.46271=440000001023=663336=3346=1273=22:27:23.0218722=0279=2269=055=USD/JPY270=27373.24271=01023=667336=3346=0273=22:27:23.021279=0269=055=USD/JPY270=27371.7271=470000001023=683336=3346=1273=22:27:23.0218722=0279=1269=055=USD/JPY270=27406.32271=870000001023=277336=3346=2273=22:27:23.0218722=0279=2269=055=USD/JPY270=27417.99271=01023=129336=3346=0273=22:27:23.030279=0269=055=USD/JPY270=27424.26271=440000001023=63336=3346=1273=22:27:23.0308722=0279=1269=055=USD/JPY270=27408.47271=860000001023=251336=3346=2273=22:27:23.0308722=0279=2269=055=USD/JPY270=27385.26271=01023=508336=3346=0273=22:27:23.030279=0269=055=USD/JPY270=27408.04271=430000001023=257336=3346=1273=22:27:23.0308722=0279=0269=055=USD/JPY270=27380.55271=440000001023=570336=3346=1273=22:27:23.0308722=0279=2269=055=USD/JPY270=27386.54271=01023=492336=3346=0273=22:27:23.030279=0269=055=USD/JPY270=27407.53271=450000001023=265336=3346=1273=22:27:23.0308722=0279=2269=055=USD/JPY270=27367.93271=01023=739336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27389271=440000001023=468336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27417.4271=01023=137336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27378.95271=410000001023=591336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27414.13271=01023=175336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27402.32271=460000001023=319336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27363.29271=01023=807336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27387.36271=410000001023=486336=3346=1273=22:27:23.0318722=0279=1269=055=USD/JPY270=27365.83271=480000001023=770336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27388.41271=01023=476336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27422.92271=460000001023=73336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27381.24271=01023=554336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27372.87271=480000001023=672336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27411.83271=01023=211336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27380.18271=400000001023=578336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27418.84271=01023=118336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27418.56271=470000001023=122336=3346=1273=22:27:23.0318722=0279=1269=055=USD/JPY270=27402.22271=410000001023=322336=3346=1273=22:27:23.0318722=0279=0269=055=USD/JPY270=27415.2271=420000001023=162336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27380.62271=01023=568336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27394.53271=420000001023=403336=3346=1273=22:27:23.0318722=0279=0269=055=USD/JPY270=27363.07271=460000001023=810336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27371.91271=01023=683336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27403.21271=460000001023=314336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27366.15271=01023=767336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27404.62271=470000001023=299336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27428.68271=01023=13336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27421.68271=440000001023=84336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27383.64271=01023=528336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27406.89271=470000001023=273336=3346=1273=22:27:23.0318722=0279=0269=055=USD/JPY270=27390.24271=480000001023=459336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27426.55271=01023=38336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27397.13271=450000001023=377336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27421.39271=01023=86336=3346=0273=22:27:23.031279=2269=055=USD/JPY270=27387.24271=01023=493336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27376.8271=420000001023=619336=3346=1273=22:27:23.0318722=0279=1269=055=USD/JPY270=27414.34271=450000001023=173336=3346=1273=22:27:23.0318722=0279=0269=055=USD/JPY270=27421.22271=470000001023=88336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27379.57271=01023=587336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27365.44271=450000001023=782336=3346=1273=22:27:23.0318722=0279=1269=055=USD/JPY270=27427.86271=410000001023=24336=3346=1273=22:27:23.0318722=0279=0269=055=USD/JPY270=27374.74271=400000001023=650336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27393.64271=01023=419336=3346=0273=22:27:23.031279=0269=055=USD/JPY270=27411.68271=480000001023=212336=3346=1273=22:27:23.0318722=0279=2269=055=USD/JPY270=27428.72271=01023=12336=3346=0273=22:27:23.032279=0269=055=USD/JPY270=27417.38271=480000001023=136336=3346=1273=22:27:23.0328722=0279=2269=055=USD/JPY270=27392.86271=01023=430336=3346=0273=22:27:23.032279=2269=055=USD/JPY270=27393.98271=01023=415336=3346=0273=22:27:23.032279=0269=055=USD/JPY270=27403.5271=440000001023=310336=3346=1273=22:27:23.0328722=0279=2269=055=USD/JPY270=27408.61271=01023=248336=3346=0273=22:27:23.032279=1269=055=USD/JPY270=27395.94271=1340000001023=392336=3346=3273=22:27:23.0328722=0279=2269=055=USD/JPY270=27421.93271=01023=78336=3346=0273=22:27:23.032279=0269=055=USD/JPY270=27394.16271=480000001023=412336=3346=1273=22:27:23.0328722=0279=2269=055=USD/JPY270=27373.42271=01023=668336=3346=0273=22:27:23.032279=0269=055=USD/JPY270=27376.74271=430000001023=619336=3346=1273=22:27:23.0328722=0279=2269=055=USD/JPY270=27385.8271=01023=505336=3346=0273=22:27:23.032279=0269=055=USD/JPY270=27386.07271=480000001023=502336=3346=1273=22:27:23.0328722=0279=1269=055=USD/JPY270=27370.71271=420000001023=702336=3346=1273=22:27:23.0328722=0279=0269=055=USD/JPY270=27375.48271=410000001023=638336=3346=1273=22:27:23.0328722=0279=2269=055=USD/JPY270=27367.54271=01023=749336=3346=0273=22:27:23.032279=0269=055=USD/JPY270=27397.96271=410000001023=372336=3346=1273=22:27:23.0328722=0279=2269=055=USD/JPY270=27387.13271=01023=494336=3346=0273=22:27:23.032279=0269=055=USD/JPY270=27369.34271=420000001023=723336=3346=1273=22:27:23.0328722=0279=0269=055=USD/JPY270=27397.22271=440000001023=377336=3346=1273=22:27:23.0328722=0279=2269=055=USD/JPY270=27414.57271=01023=168336=3346=0273=22:27:23.032279=0269=055=USD/JPY270=27417.1271=460000001023=139336=3346=1273=22:27:23.0328722=0279=2269=055=USD/JPY270=27418.39271=01023=121336=3346=0273=22:27:23.032279=0269=055=USD/JPY270=27379.95271=480000001023=582336=3346=1273=22:27:23.0328722=0279=2269=055=USD/JPY270=27362.45271=01023=821336=3346=0273=22:27:23.032279=0269=055=USD/JPY270=27415.43271=460000001023=157336=3346=1273=22:27:23.0328722=0279=2269=055=USD/JPY270=27381.68271=01023=5491023=197336=3346=1273=22:27:23.0698722=0279=2269=155=USD/JPY270=27611.88271=01023=450336=3346=0273=22:27:23.069279=1269=155=USD/JPY270=27647.01271=920000001023=28336=3346=2273=22:27:23.0698722=0279=2269=155=USD/JPY270=27627.33271=01023=270336=3346=0273=22:27:23.069279=0269=155=USD/JPY270=27589.8271=410000001023=732336=3346=1273=22:27:23.0698722=0279=2269=155=USD/JPY270=27605.53271=01023=538336=3346=0273=22:27:23.069279=0269=155=USD/JPY270=27594.08271=400000001023=681336=3346=1273=22:27:23.0698722=0279=0269=155=USD/JPY270=27590.08271=410000001023=729336=3346=1273=22:27:23.0698722=0279=2269=155=USD/JPY270=27610.17271=01023=475336=3346=0273=22:27:23.069279=0269=155=USD/JPY270=27592.31271=430000001023=706336=3346=1273=22:27:23.1868722=0279=2269=155=USD/JPY270=27605.46271=01023=552336=3346=0273=22:27:23.186279=0269=155=USD/JPY270=27594.22271=430000001023=685336=3346=1273=22:27:23.1868722=010=0259=DEXTER.1.19=14351935=X49=PARFX56=c3_user134=847252=20130408-22:27:59.607262=aquru2D00000017268=1642279=2269=055=USD/MXN270=27379.21271=01023=613336=3346=0273=22:27:22.982279=0269=055=USD/MXN270=27380.1271=480000001023=606336=3346=1273=22:27:22.9828722=0279=0269=055=USD/MXN270=27376.45271=410000001023=646336=3346=1273=22:27:22.9828722=0279=2269=055=USD/MXN270=27387.81271=01023=507336=3346=0273=22:27:23.016279=0269=055=USD/MXN270=27362.9271=430000001023=820336=3346=1273=22:27:23.0168722=0279=2269=055=USD/MXN270=27395.77271=01023=422336=3346=0273=22:27:23.016279=1269=055=USD/MXN270=27416.59271=900000001023=148336=3346=2273=22:27:23.0168722=0279=1269=055=USD/MXN270=27402.53271=410000001023=342336=3346=1273=22:27:23.0408722=0279=0269=055=USD/MXN270=27421.47271=410000001023=91336=3346=1273=22:27:23.0408722=0279=2269=055=USD/MXN270=27362.04271=01023=831336=3346=0273=22:27:23.040279=2269=055=USD/MXN270=27381.51271=01023=590336=3346=0273=22:27:23.040279=0269=055=USD/MXN270=27400.8271=400000001023=362336=3346=1273=22:27:23.0408722=0279=2269=055=USD/MXN270=27409271=01023=256336=3346=0273=22:27:23.040279=0269=055=USD/MXN270=27403.23271=420000001023=336336=3346=1273=22:27:23.0408722=0279=2269=055=USD/MXN270=27407.45271=01023=272336=3346=0273=22:27:23.040279=1269=055=USD/MXN270=27419.1271=880000001023=120336=3346=2273=22:27:23.0408722=0279=2269=055=USD/MXN270=27412.67271=01023=198336=3346=0273=22:27:23.040279=0269=055=USD/MXN270=27402.01271=410000001023=348336=3346=1273=22:27:23.0408722=0279=2269=055=USD/MXN270=27392.64271=01023=461336=3346=0273=22:27:23.040279=2269=055=USD/MXN270=27413.07271=01023=196336=3346=0273=22:27:23.040279=0269=055=USD/MXN270=27393.88271=420000001023=445336=3346=1273=22:27:23.0408722=0279=2269=055=USD/MXN270=27370.76271=01023=701336=3346=0273=22:27:23.047279=2269=055=USD/MXN270=27414.74271=01023=174336=3346=0273=22:27:23.047279=0269=055=USD/MXN270=27418.51271=410000001023=128336=3346=1273=22:27:23.0478722=0279=2269=055=USD/MXN270=27407.91271=01023=267336=3346=0273=22:27:23.047279=1269=055=USD/MXN270=27371.75271=850000001023=691336=3346=2273=22:27:23.0478722=0279=2269=055=USD/MXN270=27392.26271=01023=464336=3346=0273=22:27:23.047279=0269=055=USD/MXN270=27379.5271=470000001023=606336=3346=1273=22:27:23.0478722=0279=2269=055=USD/MXN270=27372.2271=01023=685336=3346=0273=22:27:23.048279=0269=055=USD/MXN270=27382.86271=430000001023=566336=3346=1273=22:27:23.0488722=0279=2269=055=USD/MXN270=27388.84271=01023=495336=3346=0273=22:27:23.048279=0269=055=USD/MXN270=27427.92271=440000001023=22336=3346=1273=22:27:23.0488722=0279=2269=055=USD/MXN270=27394.39271=01023=438336=3346=0273=22:27:23.048279=0269=055=USD/MXN270=27413.33271=440000001023=194336=3346=1273=22:27:23.0488722=0279=2269=055=USD/MXN270=27428.59271=01023=9336=3346=0273=22:27:23.048279=0269=055=USD/MXN270=27399.7271=410000001023=375336=3346=1273=22:27:23.0488722=0279=2269=055=USD/MXN270=27362.95271=01023=815336=3346=0273=22:27:23.048279=0269=055=USD/MXN270=27362.65271=470000001023=820336=3346=1273=22:27:23.0488722=0279=2269=055=USD/MXN270=27387.39271=01023=510336=3346=0273=22:27:23.048279=2269=055=USD/MXN270=27370.5271=01023=702336=3346=0273=22:27:23.048279=0269=055=USD/MXN270=27421.64271=450000001023=91336=3346=1273=22:27:23.0488722=0279=0269=055=USD/MXN270=27379.76271=470000001023=604336=3346=1273=22:27:23.0488722=0279=2269=055=USD/MXN270=27384.65271=01023=533336=3346=0273=22:27:23.048279=0269=055=USD/MXN270=27393.78271=430000001023=449336=3346=1273=22:27:23.0488722=0279=0269=055=USD/MXN270=27427.43271=460000001023=26336=3346=1273=22:27:23.0488722=0279=0269=055=USD/MXN270=27367.2271=470000001023=759336=3346=1273=22:27:23.0488722=0279=2269=055=USD/MXN270=27364.08271=01023=797336=3346=0273=22:27:23.048279=0269=055=USD/MXN270=27370.92271=480000001023=702336=3346=1273=22:27:23.0488722=0279=0269=055=USD/MXN270=27363.19271=400000001023=813336=3346=1273=22:27:23.0488722=0279=2269=055=USD/MXN270=27363.46271=01023=810336=3346=0273=22:27:23.048279=0269=055=USD/MXN270=27407.56271=470000001023=272336=3346=1273=22:27:23.0488722=0279=0269=055=USD/MXN270=27393.97271=460000001023=448336=3346=1273=22:27:23.0488722=0279=2269=055=USD/MXN270=27403.04271=01023=337336=3346=0273=22:27:23.048279=2269=055=USD/MXN270=27413.94271=01023=188336=3346=0273=22:27:23.048279=2269=055=USD/MXN270=27428.66271=01023=8336=3346=0273=22:27:23.048279=2269=055=USD/MXN270=27406.51271=01023=282336=3346=0273=22:27:23.048279=2269=055=USD/MXN270=27368.62271=01023=741336=3346=0273=22:27:23.048279=1269=055=USD/MXN270=27403.45271=900000001023=331336=3346=2273=22:27:23.0488722=0279=0269=055=USD/MXN270=27387.94271=470000001023=505336=3346=1273=22:27:23.0488722=01023=590336=3346=0273=22:27:23.243279=0269=155=USD/MXN270=27638.8271=470000001023=115336=3346=1273=22:27:23.2438722=0279=1269=155=USD/MXN270=27632.43271=400000001023=189336=3346=1273=22:27:23.2438722=0279=0269=155=USD/MXN270=27647.52271=430000001023=23336=3346=1273=22:27:23.2438722=0279=2269=155=USD/MXN270=27622.85271=01023=299336=3346=0273=22:27:23.243279=0269=155=USD/MXN270=27624.18271=460000001023=274336=3346=1273=22:27:23.2438722=0279=2269=155=USD/MXN270=27610.29271=01023=465336=3346=0273=22:27:23.243279=0269=155=USD/MXN270=27626.95271=440000001023=242336=3346=1273=22:27:23.2438722=0279=1269=155=USD/MXN270=27648.18271=460000001023=12336=3346=1273=22:27:23.2448722=0279=0269=155=USD/MXN270=27621.89271=420000001023=316336=3346=1273=22:27:23.2448722=0279=2269=155=USD/MXN270=27628.57271=01023=229336=3346=0273=22:27:23.244279=0269=155=USD/MXN270=27636.79271=410000001023=142336=3346=1273=22:27:23.2448722=0279=2269=155=USD/MXN270=27637.38271=01023=133336=3346=0273=22:27:23.244279=0269=155=USD/MXN270=27602.75271=400000001023=565336=3346=1273=22:27:23.2448722=0279=0269=155=USD/MXN270=27620.37271=440000001023=338336=3346=1273=22:27:23.2448722=0279=2269=155=USD/MXN270=27630.68271=01023=209336=3346=0273=22:27:23.244279=0269=155=USD/MXN270=27636.13271=850000001023=150336=3346=2273=22:27:23.2448722=0279=1269=155=USD/MXN270=27610.2271=400000001023=468336=3346=1273=22:27:23.2448722=0279=1269=155=USD/MXN270=27624.31271=480000001023=271336=3346=1273=22:27:23.2448722=0279=2269=155=USD/MXN270=27605.29271=01023=532336=3346=0273=22:27:23.244279=1269=155=USD/MXN270=27601.37271=920000001023=583336=3346=2273=22:27:23.2448722=0279=2269=155=USD/MXN270=27635.6271=01023=158336=3346=0273=22:27:23.244279=1269=155=USD/MXN270=27601.6271=890000001023=579336=3346=2273=22:27:23.2448722=0279=2269=155=USD/MXN270=27602.71271=01023=565336=3346=0273=22:27:23.244279=0269=155=USD/MXN270=27600.74271=430000001023=591336=3346=1273=22:27:23.2448722=0279=1269=155=USD/MXN270=27623.63271=400000001023=284336=3346=1273=22:27:23.3238722=0279=0269=155=USD/MXN270=27602.66271=400000001023=566336=3346=1273=22:27:23.3238722=010=012
132157:8473:OUT 20130408-22:27:59.611 : 9=DEXTER.1.19=13213035=X49=PARFX56=c3_user134=847352=20130408-22:27:59.611262=aquru2D00000002268=1509279=2269=055=EUR/CHF270=27379.5271=01023=598336=3346=0273=22:27:23.084279=0269=055=EUR/CHF270=27396.49271=440000001023=414336=3346=1273=22:27:23.0848722=0279=2269=055=EUR/CHF270=27404.86271=01023=305336=3346=0273=22:27:23.085279=0269=055=EUR/CHF270=27374.86271=400000001023=668336=3346=1273=22:27:23.0858722=0279=2269=055=EUR/CHF270=27416.14271=01023=174336=3346=0273=22:27:23.085279=2269=055=EUR/CHF270=27411.65271=01023=233336=3346=0273=22:27:23.085279=0269=055=EUR/CHF270=27363.95271=440000001023=808336=3346=1273=22:27:23.0858722=0279=2269=055=EUR/CHF270=27408.47271=01023=262336=3346=0273=22:27:23.098279=0269=055=EUR/CHF270=27400.23271=410000001023=371336=3346=1273=22:27:23.098
Heres a simple regex that would do it:
9=DEXTER((?s:.)*?)((?=9=DEXTER)|$)
Basically capture a group ((?s:.)*?) between a 9=DEXTER OR end of file ($)
String regex = "9=DEXTER((?s:.)*?)((?=9=DEXTER)|$)";
String text = "9=DEXTERHello9=DEXTERworld\nnewline9=DEXTERfoo9=DEXTERbar";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(text);
while (m.find()) {
String s = m.group(1);
System.out.println(":"+s);
}
output:
:Hello
:world
new line
:foo
:bar

Extracting part of URL using java regular expression

I'm trying to extract part of the URL in the text files.
for example:
/p/gnomecatalog/bugs/search/?q=status%3Aclosed-accepted+or+status%3Awont-fix+or+status%3Aclosed" class="search_bin"><span>Closed Tickets</span></a>
I would like to extract only
/p/gnomecatalog/bugs/search/?q=status%3Aclosed-accepted+or+status%3Awont-fix+or+status%3Aclosed
HOW I COULD DO THAT BY USING REGULAR Expression. I tried with regex
"/p/*./bugs/*."
but it didn't work.
Try this:
"\/p.*\/bugs[^"]*"
it means: "/p"
then: all chars,
then: "/bugs",
then: all chars except "
You can use :
(\/p\/.*\/bugs\/.*?(?="))
Java Code :
String REGEX = "(\\/p\\/.*\\/bugs\\/.*?(?=\"))";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(line);
while (m.find()) {
String matched = m.group();
System.out.println("Mached : "+ matched);
}
OUTPUT
Mached : /p/gnomecatalog/bugs/search/?q=status%3Aclosed-accepted+or+status%3Awont-fix+or+status%3Aclosed
DEMO
Explanation:
Here's another way:
(?i)/p/[a-z/]+bugs/[^ "]+
The (?i) in the beginning makes the regex case insensitive so you don't have to worry about that. Then after bugs/ it will continue until it reaches either a space or a ".

regex: Java: match word between 2 spaces

How can I extract the "id" from the following string using regex.
string = 11,"col=""book"" id=""title"" length=""10""
I need to be able to extract the "id" header along with the value "title".
outcome: id=""title""
I am trying to the use split function with a regex to extract the identifier from the string.
Try this:
String result = "col=\"book\" id=\"title\" length=\"10\"";
String pattern = ".*(id\\s*=\\s*\"[^\"]*\").*";
System.out.println(result.replaceAll(pattern,"$1"));
Cheers!
Use Pattern and Matcher classes to find what you are looking for. Try to find these regex \\bid=[^ ]*.
String data = "string = 11,\"col=\"\"book\"\" id=\"\"title\"\" length=\"\"10\"\"";
Matcher m = Pattern.compile("\\bid=[^ ]*").matcher(data);
if (m.find())
System.out.println(m.group());

Categories

Resources