part String at particular position (#17. and #5.) - java

I have the following gps position String
#114.034407,E,22.648272,N,0.00,0.00#010104#004500#17.034407,E,22.648272,N,0.00,0.00#010104#004500#5.034407,E,22.648272,N,0.00,0.00#010104#004500
and I want to part it the longitude position (#17.,#5.). All longitudes position starts with # and contains . point after the fitst or first and second or first, second and third. How can I get this result with regular expression?
Result:
#114.034407,E,22.648272,N,0.00,0.00#010104#004500
#17.034407,E,22.648272,N,0.00,0.00#010104#004500
#5.034407,E,22.648272,N,0.00,0.00#010104#004500
Code
Pattern pattern = Pattern.compile("regular expression");
Matcher matcher = pattern.matcher(gpsPacket);
matcher.matches();

You can use this regex:
(?=#\d{1,3}\.)
with this code:
import java.util.*;
import java.lang.*;
import java.io.*;
class YourClass
{
public static void main (String[] args) throws java.lang.Exception
{
String str = "#114.034407,E,22.648272,N,0.00,0.00#010104#004500#17.034407,E,22.648272,N,0.00,0.00#010104#004500#5.034407,E,22.648272,N,0.00,0.00#010104#004500";
String delimiters = "(?=#\\d{1,3}\\.)";
String[] coordinates = str.split(delimiters);
for(String coordinate : coordinates) {
System.out.println(coordinate);
}
}
}
It will split the string on a # followed by 1 to 3 number then a dot. Live demo

This is the regex based upon the information given.
It pulls the three data sets out of your input.
If there are more rules than what you have minimally specified you will have to make adjustments.
I suggest going to a website and practicing. There is a lot of helpful information here RegExr, along with the ability to practice live.
(#[\d]{1,3}.[\d]{6},E,[\d]{2}.[\d]{6},N,[\d]{1}.[\d]{2},[\d]{1}.[\d]{2}#[\d]{6}#[\d]{6})

Related

is there any way i can include space of specific word both at front and end using regex

I am trying to format one word in a string and want to add space both at front and end if its not present .Is there any way to achieve the same.
Code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String test = "";
String in = "Hi UimIMAGE [rofl]test IMAGE [rofl] notify the bull";
test = in.replaceAll("IMAGE \\[(.*?)\\]", "$1");
//String.format("%1$" + (1) + "s", str);
System.out.println(test);
}
}
As of now If i run this I see it just return me .
Hi Uimrofltest rofl notify the bull
I instead wanted a regex which will add space before and after my given regex if the space not there .Is there anything I can do without manuplulating the string manually .
Expected output
Hi Uim rofl test rofl notify the bull
You can try using String#replaceAll here:
String in = "Hi UimIMAGE [rofl]test IMAGE [rofl] notify the bull";
String out = in.replaceAll("\\s*IMAGE \\[(.*?)\\]\\s*", " $1 ");
System.out.println(in);
System.out.println(out);
This prints:
Hi UimIMAGE [rofl]test IMAGE [rofl] notify the bull
Hi Uim rofl test rofl notify the bull
The idea behind the regex is to capture IMAGE [something] tags, and then replace with just the contents in square brackets. Note that we also capture any optional whitespace on either ends, and then pad the replacement with just a single space on both ends, to avoid unwanted extra whitespace.

JAVA Regex - How do I filter out entire strings with lengths that are greater than a certain number? [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 3 years ago.
My program is used to filter out names starting with a capital letter [A-M] and to filter out any names with a length less than 5 and greater than 9. The code does filter out the names with a length less than 5, but when I input a name with a length greater than 9, it just cuts off the rest of the name.
Ex: Bartholomew would cut off to Bartholom rather than just not using Bartholomew.
I have tried to move the length flag to different spots in the regex field. Other than that, I do not know regex well enough to try much more. As for putting these strings into another function just to test the lengths - I am trying to make it in one regex field.
import java.io.File;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Egor {
public static void main(String args[]) throws Exception{
Scanner s = new Scanner(new File("Names.dat"));
String[] names = new String[30];
int i = 0;
while(s.hasNext()) {
names[i] = s.next();
i++;
}
String store = "";
for (String str: names) {
store = store + str + " ";
}
System.out.println(store);
Pattern checkName = Pattern.compile("([A-M][a-z]{5,9})");
Matcher matcher = checkName.matcher(store);
System.out.println("-----------------------");
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
The expected should print out names like - Ashley, Brendan, Henry would print out
The unexpected is that names like - Bartholomew, with lengths greater than 9 print out to Bartholom
You need to add a positive look behind and positive look ahead and positive look behind for the desired characters that separate your names. Based on your code it looks like that would be a start of string anchor or space, and a end of string anchor or space for the look behind and look ahead respectively. Will look something like this:
(?<=\b)([A-M][a-z]{5,9})(?=\b)
Look ahead and look behinds in regex match what is ahead of and behind, but do not include it in the matched result.

Convert this pattern to regex for Pattern.matches(..)

Some of my strings may contain a substring that looks like #[alph4Num3ric-alph4Num3ric] , where I will find the alpha numberic id and replace it with a corresponding text value mapped to the associated key in a map.
My first inclination was to check if my string.contains("#[") but I want to be more specific
so now I am looking at Pattern.matches( but am unsure of the regex and total expression
how would I regex for #[ ...... - .... ] in the Pattern.matches method, it must also account for dashes. So I'm not sure what needs to be escaped in this syntax or wildcarded, or more.
I am also not 100% sure if this is the best message. I want to get a boolean from Pattern.matches first, and then get the real value and modify the string with those values, which seems good enough, but I want to minimize computations.
Plese try this ,
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String expression = "String contains #[alph4Num3ric-alph4Num3ric] as substring";
Pattern pattern = Pattern
.compile("\\#\\[([a-zA-Z0-9]+)-([a-zA-Z0-9]+)\\]");
Matcher matcher = pattern.matcher(expression);
while (matcher.find()) {
System.out.println("matched: "+matcher.group());
System.out.println("group1: "+matcher.group(1));
System.out.println("group2: "+matcher.group(2));
System.out
.println("after replace "+expression.replace(matcher.group(1), "customkey"));
}
}
}
output :
matched: #[alph4Num3ric-alph4Num3ric]
group1: alph4Num3ric
group2: alph4Num3ric
after replace: String contains #[customkey-customkey] as substring
Try using this:
/#[(a-zA-Z0-9-)+]/
I haven't given it a try but hope this would help. Also if it returns an error then add a backward slash between 9 and - e.g. /#[(a-zA-Z0-9-)+]/

How write in java regex any string of characters?

I have a text and I'd like to write a regular expression to extract the string after second #. For example:
# some text with letter, digit 123 1234 and symbols {[ #text_to_extract.
How would I write a regular expression to extract only the string after second #. This code seems like a step in the right direction:
Pattern p = Pattern.compile("##(.+?)");
Matcher m = p.matcher("asdasdas##textToExtract");
This works when text between # is empty, but how do I specify any text in a regex?
Pattern.compile("#(*)#(.+?)"); ?
Edited:
One more condition, text can be between # and # but doesn't have to.
Don't capture the first group
Change the plain * to .*.
Make the second wildcard greedy, since it will otherwise capture only a single character
Pattern.compile("#.*#(.+)");
The "non-greedy" operator should be removed. (.*?) should be (.*) ... Otherwise you match just the minimum of the text after the second #. Definitely need a "." in front of the *. It means "0 or more of the proceeding character. Actually, maybe you want [^#]* instead... so it matches anything but the at symbol.. so you're guaranteed to get everything, even if . doesn't match newlines. Anyway, here's working code.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.*;
class Ideone {
public static void main(String[] args) throws java.lang.Exception {
// Pattern p = Pattern.compile("#(*)#(.+?)");
Pattern p = Pattern.compile("#.*#(.+)");
Matcher m = p.matcher("asdasdas##textToExtract");
while (m.find()) {
System.out.println(m.group(1));
}
}
}
Play with the code here: http://ideone.com/rxB5Zy
You should do it this way
Matcher m =Pattern.compile("^[^#]*#[^#]*#([^#]*)").matcher(input);

split a string based on parentheses and next characther

I am having a problem tring to split a sting based on parentheses.
I have a String like this
Fe(C5H5)2FeO3 and I need to split the sting in to an array so the array reads
Fe
(C5H5)2
FeO3
Im an using this code.
String form = "Fe(C5H5)2FeO3";
from.split([()]+);
I am having trouble getting the characther after the ")" to split out.
This also has to work for multiple sets of () in the same string.
Thanks
positive look ahead and look behind can do some of this:
String formula = "Fe(C5H5)2FeO3";
String regex = "(?=\\()|(?<=\\)\\d)";
String[] tokens = formula.split(regex );
System.out.println(Arrays.toString(tokens));
For more on this, check out the regular expressions tutorial
You can use a simple regex to match parts of the sequence instead of splitting on a regex:
import java.util.*;
import java.util.regex.*;
import java.lang.*;
class Main {
public static void main (String[] args) throws java.lang.Exception
{
String formula = "Fe(C5H5)2FeO3";
Pattern p = Pattern.compile("[^(]+|[(][^)]*[)]\\d+");
Matcher m = p.matcher(formula);
while (m.find()) {
System.out.println(m.group());
}
}
}
This program produces the output below:
Fe
(C5H5)2
FeO3

Categories

Resources