Extract given substring from a paragraph - java

I want to perform the following functionality :
From a given paragraph extract the given String, like
String str= "Hello this is paragraph , Ali#yahoo.com . i am entering random email here as this one AHmar#gmail.com " ;
What I have to do is to parse the whole paragraph, read the Email address, and print their server names , i have tried it using for loop with substring method , did use indexOf , but might be my logic is not that good to get it , can someone help me with it please?

You need to use Regular Expression for this case.
Try the below Regex: -
String str= "Hello this is paragraph , Ali#yahoo.com . i am " +
"entering random email here as this one AHmar#gmail.com " ;
Pattern pattern = Pattern.compile("#(\\S+)\\.\\w+");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
OUTPUT: -
yahoo
gmail
UPDATE: -
Here's the code with substring and indexOf: -
String str= "Hello this is paragraph , Ali#yahoo.com . i am " +
"entering random email here as this one AHmar#gmail.com " ;
while (str.contains("#") && str.contains(".")) {
int index1 = str.lastIndexOf("#"); // Get last index of `#`
int index2 = str.indexOf(".", index1); // Get index of first `.` after #
// Substring from index of # to index of .
String serverName = str.substring(index1 + 1, index2);
System.out.println(serverName);
// Replace string by removing till the last #,
// so as not to consider it next time
str = str.substring(0, index1);
}

You need to use a regular expression to extract the email. Start off with this test harness code. Next, construct your regular expression and you should be able to extract the email address.

Try this:-
String e= "Hello this is paragraph , Ali#yahoo.com . i am entering random email here as this one AHmar#gmail.comm";
e= e.trim();
String[] parts = e.split("\\s+");
for (String e: parts)
{
if(e.indexOf('#') != -1)
{
String temp = e.substring(e.indexOf("#") + 1);
String serverName = temp.substring(0, temp.indexOf("."));
System.out.println(serverName); }}

Related

JAVA Get text from String

Hi I get this String from server :
id_not="autoincrement"; id_obj="-"; id_tr="-"; id_pgo="-"; typ_not=""; tresc="Nie wystawił"; datetime="-"; lon="-"; lat="-";
I need to create a new String e.x String word and send a value which I get from String tresc="Nie wystawił"
Like #Jan suggest in comment you can use regex for example :
String str = "id_not=\"autoincrement\"; id_obj=\"-\"; id_tr=\"-\"; id_pgo=\"-\"; typ_not=\"\"; tresc=\"Nie wystawił\"; datetime=\"-\"; lon=\"-\"; lat=\"-\";";
Pattern p = Pattern.compile("tresc(.*?);");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(m.group());
}
Output
tresc="Nie wystawił";
If you want to get only the value of tresc you can use :
Pattern p = Pattern.compile("tresc=\"(.*?)\";");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(m.group(1));
}
Output
Nie wystawił
Something along the lines of
Pattern p = Pattern.compile("tresc=\"([^\"]+)\");
Matcher m = p.matcher(stringFromServer);
if(m.find()) {
String whatYouWereLookingfor = m.group(1);
}
should to the trick. JSON parsing might be much better in the long run if you need additional values
Your question is unclear but i think you get a string from server and from that string you want the string/value for tresc. You can first search for tresc in the string you get. like:
serverString.substring(serverString.indexOf("tresc") + x , serverString.length());
Here replace x with 'how much further you want to pick characters.
Read on substring and delimiters
As values are separated by semicolon so annother solution could be:
int delimiter = serverstring.indexOf(";");
//in string thus giving you the index of where it is in the string
// Now delimiter can be -1, if lets say the string had no ";" at all in it i.e. no ";" is not found.
//check and account for it.
if (delimiter != -1)
String subString= serverstring.substring(5 , iend);
Here 5 means tresc is on number five in string, so it will five you tresc part.
You can then use it anyway you want.

Replace characters in a String, in a specific location

I have the following string;
String s = "Hellow world,how are you?\"The other day, where where you?\"";
And I want to replace the , but only the one that is inside the quotation mark \"The other day, where where you?\".
Is it possible with regex?
String s = "Hellow world,how are you?\"The other day, where where you?\"";
Pattern pattern = Pattern.compile("\"(.*?)\"");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
s = s.substring(0, matcher.start()) + matcher.group().replace(',','X') +
s.substring(matcher.end(), s.length());
}
If there are more then two quotes this splits the text into in quote/out of quote and only processes inside quotes. However if there are odd number of quotes (unmatched quotes), the last quote is ignored.
If you are sure this is always the last "," you can do that
String s = "Hellow world,how are you?\"The other day, where where you?\"";
int index = s.lastIndexOf(",");
if( index >= 0 )
s = new StringBuilder(s).replace(index , index + 1,"X").toString();
System.out.println(s);
Hope it helps.

How to search word in String text, this word end "." or "," in java

someone can help me with code?
How to search word in String text, this word end "." or "," in java
I don't want search like this to find it
String word = "test.";
String wordSerch = "I trying to tasting the Artestem test.";
String word1 = "test,"; // here with ","
String word2 = "test."; // here with "."
String word3 = "test"; //here without
//after i make string array and etc...
if((wordSearch.equalsIgnoreCase(word1))||
(wordSearch.equalsIgnoreCase(word2))||
(wordSearh.equalsIgnoreCase(word3))) {
}
if (wordSearch.contains(gramer))
//it's not working because the word Artestem will contain test too, and I don't need it
You can use the matches(Regex) function with a String
String word = "test.";
boolean check = false;
if (word.matches("\w*[\.,\,]") {
check = true;
}
You can use regex for this
Matcher matcher = Pattern.compile("\\btest\\b").matcher(wordSearch);
if (matcher.find()) {
}
\\b\\b will match only a word. So "Artestem" will not match in this case.
matcher.find() will return true if there is a word test in your sentence and false otherwise.
String stringToSearch = "I trying to tasting the Artestem test. test,";
Pattern p1 = Pattern.compile("test[.,]");
Matcher m = p1.matcher(stringToSearch);
while (m.find())
{
System.out.println(m.group());
}
You can transform your String in an Array divided by words(with "split"), and search on that array , checking the last character of the words(charAt) with the character that you want to find.
String stringtoSearch = "This is a test.";
String whatIwantToFind = ",";
String[] words = stringtoSearch.split("\\s+");
for (String word : words) {
if (whatIwantToFind.equalsignorecas(word.charAt(word.length()-1);)) {
System.out.println("FIND");
}
}
What is a word? E.g.:
Is '5' a word?
Is '漢語' a word, or two words?
Is 'New York' a word, or two words?
Is 'Kraftfahrzeughaftpflichtversicherung' (meaning "automobile liability insurance") a word, or 3 words?
For some languages you can use Pattern.compile("[^\\p{Alnum}\u0301-]+") for split words. Use Pattern#split for this.
I think, you can find word by this pattern:
String notWord = "[^\\p{Alnum}\u0301-]{0,}";
Pattern.compile(notWord + "test" + notWord)`
See also: https://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

Check if id in string and get value if so

I am trying to get a regex to match, then get the value with it. For example, I want to check for 1234 as an id and if present, get the status (which is 0 in this case). Basically its id:status. Here is what I am trying:
String topicStatus = "1234:0,567:1,89:2";
String someId = "1234";
String regex = "\\b"+someId+":[0-2]\\b";
if (topicStatus.matches(regex)) {
//How to get status?
}
Not only do I not know how to get the status without splitting and looping through, I don't know why it doesn't match the regex.
Any help would be appreciated. Thanks.
Use the Pattern class
String topicStatus = "1234:0,567:1,89:2";
String someId = "1234";
String regex = "\\b"+someId+":[0-2]\\b";
Pattern MY_PATTERN = Pattern.compile(regex);
Matcher m = MY_PATTERN.matcher(topicStatus);
while (m.find()) {
String s = m.group(1);
System.out.println(s);
}
The key here is to surround the position you want [0-2] in parenthesis which means it will be saved as the first group. You then access it through group(1)
I made some assumptions that your pairs we're always comma separate and then delimited by a colon. Using that I just used split.
String[] idsToCheck = topicStatus.split(",");
for(String idPair : idsToCheck)
{
String[] idPairArray = idPair.split(":");
if(idPairArray[0].equals(someId))
{
System.out.println("id : " + idPairArray[0]);
System.out.println("status: " + idPairArray[1]);
}
}

How to remove the second substring with regex?

String text;
System.out.println(text);
In the console it looks like this:
The US.....................................
Illinois Commerce .......... ..............
...........................................
..........................Illinois Commerce
I need to get rid of the second substring Illinois Commerce
This is what I tried:
text = text.replaceAll("(?:Illinois Commerce:.*?){2}", "");
I get java.lang.ArrayIndexOutOfBoundsException: 1
You can try this:
text = text.replaceFirst("(Illinois Commerce(?s).*?)Illinois Commerce", "$1");
This should do it assuming it is following by whitespace or end of the string.
text = text.replaceAll("Illinois Commerce(?= ?$)", "");
Or the following will work for this case.
text = text.replaceAll("\bIllinois Commerce\s*$", "");
I would not use regex for this. What I would do is:
Find the index of first occurrence of "Illinois Commerce"
Get the substring from index + 1 till the end.
Replace the "Illinois Commerce" in that substring. That will make sure that I don't replace the 1st occurrence, because it will not be fully available in this substring.
Then concatenate the first part of the string with the resultant substring.
This is how the code would go like:
int index = text.indexOf("Illinois Commerce");
String result = text.substring(0, index + 1) +
text.substring(index + 1).replace("Illinois Commerce", "");
System.out.println(result);
text.substring(0, index + 1) will take the string till the I of the first Illi.....
text.substring(index + 1) will start from l of first Illi.... till the end of the string. So, the only string to replace is the 2nd occurrence.
Since there are only two occurrences, lastIndexOf may be better than a regex for this case.
Anyway, below are the regx and the lastIndexOf way to do it.
public static void main(String[] args) {
String test = "The US.....................................\n" +
"Illinois Commerce .......... ..............\n" +
"...........................................\n" +
"..........................Illinois Commerce \n";
String toFind = "Illinois Commerce";
System.out.print("regex\n");
System.out.println(test.replaceAll( "(?s)^(.*)"+toFind+"(.*)$", "$1$2" ));
System.out.print("\nlastIndexOf\n");
int start = test.lastIndexOf(toFind);
System.out.println( test.substring( 0, start)
+ test.substring(start+toFind.length()));
}

Categories

Resources