I have a string template that looks something like this:
This position is reserved for <XXXXXXXXXXXXXXXXXXXXXXXXXXX>. Start date is <XXXXXXXX>
Filled out, this might look like this (fixed width is preserved):
This position is reserved for <JOHN SMITH >. Start date is <20150228>
How can I extract multiple differences in a single String? I don't want to use an entire templating engine for one task if I can avoid it.
You can try regex like this :
public static void main(String[] args) {
String s = "This position is reserved for <JOHN SMITH >. Start date is <20150228>";
Pattern p = Pattern.compile(".*?<(.*?)>.*<(.*?)>");
Matcher m = p.matcher(s);
while(m.find()){
System.out.println("Name : " +m.group(1).trim());
System.out.println("Date : " +m.group(2).trim());
}
}
O/P :
Name : JOHN SMITH
Date : 20150228
If the template might be modified you could use a format pattern.
String expected = "This position is reserved for <JOHN SMITH >. Start date is <20150228>";
System.out.println(expected);
// define the output format
String template = "This position is reserved for <%-27s>. Start date is <%s>";
String name = "JOHN SMITH";
String startDate = "20150228";
// output the values using the defined format
System.out.println(String.format(template, name, startDate));
Related
If I have a string like :
String str = "startDate:23/04/2016;endDate:;renewDate;"
Required multiple operation on the string.I need to get a String and extract different sub-strings using a delimiter (";") then again extract different sub-strings to form a key-value using a delimiter (":"). If against the key their is no value present the update with diff-diff default value like ("01/01/2000","01/01/1900" since both are of type Date).
if you notice for renewDate field their is no separator (":") in this case we need to append the separator along with default value (:01/01/1900) so that my expected result would be like :
String str = "startDate:23/04/2016;endDate:01/01/2000;renewDate:01/01/1900;"
I have tried below regex but it is not working for all scenario :
String regExpression = "(?<=\\bendDate:)[;]+"
str = str.replaceAll(regExpression , "01/01/2000");
Can any one guide me how to achive the result using regex.Thanks!!!!!
As Tim said we can use regex pattern to replace the value. Adding to his answer, the only change I recommend to add colon(:) as optional in pattern used for both renewDate and endDate in text.
String endDate = "01/01/2000";
String renewDate = "01/01/1900";
String str = "startDate:23/04/2016;endDate:;renewDate;";
str = str.replaceAll(";endDate:?;", ";endDate:"+endDate+";");
str = str.replaceAll(";renewDate:?;", ";renewDate:"+renewDate+";");
System.out.println(str);
This will give the output as below
startDate:23/04/2016;endDate:01/01/2000;renewDate:01/01/1900;
Using a regex replacement, we can try:
String endDate = "01/01/2000";
String renewDate = "01/01/1900";
String str = "startDate:23/04/2016;endDate:;renewDate:;";
String output = str.replaceAll("\\bendDate:;", "endDate:" + endDate + ";")
.replaceAll("\\brenewDate:;", "renewDate:" + renewDate + ";");
System.out.println(output);
This prints:
startDate:23/04/2016;endDate:01/01/2000;renewDate:01/01/1900;
The logic here is to only target empty end and renew date fields, including default values if needed.
Alternatively, separate strings based on your first delimiter ; and then :.
String str = "startDate:23/04/2016;endDate:;renewDate;";
String dates[] = str.split(";");
for(String date : dates){
String default[] = date.split(":");
output.append(default[0] + ":" + (default.length > 1 ? default[1] : defaultDate));
}
Is there any way I could select specific text after specific text and keep selecting until that word is selected. And once selected then leave the remaining.
Here is the example
ABCDEF
JHJHJNJN<098978686
<jjg>
HGHJFGV XXXX
10-10-2018
JHKGHKGHG
JKHJHHJM
10-10-2019 JGHHGHGVH
HBVJHBHBB
Just want to select this date 10-10-2018 in whole content which always comes after XXX with couple of spaces. I can't use just regex with specific value(10-10-2018) because date can be changed and possible that date pattern somewhere is also present like in example in last line.
Please share your thoughts..!
Thanks
Assuming the example is correct, then the following regex will extract just the date using find() and ensure that DOTALL is set.
"XXXX.*?[\\s]+([\\d]{1,2}-[\\d]{1,2}-[\\d]{4})"
Basically, search for XXX followed by spaces/newline then find the date. It will be placed into a group and can then be extracted.
You can see the operation at this location, though be sure to select "DOTALL".
public String getDate(String input)
{
String date = "";
Pattern dte = Pattern.compile("XXXX.*?[\\s]+([\\d]{1,2}-[\\d]{1,2}-[\\d]{4})", Pattern.DOTALL);
Matcher m = dte.matcher(input);
if (m.find() && m.groupCount() > 0) {
date = m.group(1);
}
return date;
}
Test case
#Test
public void testData() throws Exception
{
RegEx_52879334 re = new RegEx_52879334();
String input = re.getInputData();
String date = re.getDate(input);
assertEquals("10-10-2018", date);
System.out.println("Found: " + date);
}
Output:
Found: 10-10-2018
I am receiving metainformations in a radio player via ICY.
Here is a short example of how this can look:
die neue welle - Der beste Musikmix aus 4 Jahrzehnten! - WELSHLY ARMS - SANCTUARY - Der Mehr Musik-Arbeitstag mit Benni Rettich
Another example for the meta information stream would be:
SWR1 Baden Württemberg
or
Welshly Arms - Sanctuary
Now I need to extract the title from there, the problem is that this 'meta-information' string can have any format.
What I know:
-I know the complete meta information string as showed in the first code section
-I know the station name, which is delivered by another ICY propertie
The first approach was to check if the string contains the station name (I thought if not, it has to be the title):
private boolean icyInfoContainsTitleInfo() {
String title = id3Values.get("StreamTitle"); //this is the title string
String icy = id3Values.get("icy-name"); //this is the station name
String[] titleSplit = title.split("\\s");
String[] icySplit = icy.split("\\s");
for (String a : titleSplit) {
StringBuilder abuilder = new StringBuilder();
abuilder.append(a);
for (String b : icySplit) {
StringBuilder builder = new StringBuilder();
builder.append(b);
if (builder.toString().toLowerCase().contains(abuilder.toString().toLowerCase())) {
return false;
}
}
}
return true;
}
But that does not help me if title and station are both present in the title string.
Is there a pattern that matches a string followed by a slash, backslash or a hyphen followed by another string?
Has anyone encountered a similiar problem?
Since you don't have a specification and each station can send a different format. I would not try to find a "perfect" pattern but simply create a mapping to store each station's format regex to recover the title.
First, create a map
Map<String, String> stationPatterns = new HashMap<>();
Them, insert some pattern you know
stationPatterns.put("station1", "(.*)");
stationPatterns.put("station2", "station2 - (.*)");
...
Then, you just need to get this pattern (where you ALWAYS find one capture group).
public String getPattern(String station){
return stationPatterns.getOrDefault(station, "(.*)"); //Use a default value to get everything)
}
With this, you just need to get a pattern to extract the title from a String.
Pattern pattern = Pattern.compile(getPattern(stationSelected));
Matcher matcher = pattern.matcher(title);
if (matcher.find()) {
System.out.println("Title : " + matcher.group(1));
} else {
System.err.println("The title doesn't match the format");
}
I know there are similar questions regarding to this. However, I tried many solutions and it just does not work for me.
I need help to extract multiple substrings from a string:
String content = "Ben Conan General Manager 90010021 benconan#gmail.com";
Note: The content in the String may not be always in this format, it may be all jumbled up.
I want to extract the phone number and email like below:
1. 90010021
2. benconan#gmail.com
In my project, I was trying to get this result and then display it into 2 different EditText.
I have tried using pattern and matcher class but it did not work.
I can provide my codes here if requested, please help me ~
--------------------EDIT---------------------
Below is my current method which only take out the email address:
private static final String EMAIL_PATTERN =
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\#" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+";
public String EmailValidator(String email) {
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
if (matcher.find()) {
return email.substring(matcher.start(), matcher.end());
} else {
// TODO handle condition when input doesn't have an email address
}
return email;
}
You can separate your string into arraylist like this
String str = "Ben Conan, General Manager, 90010021, benconan#gmail.com";
List<String> List = Arrays.asList(str.split(" "));
maybe you should do this instead of yours :
String[] Stringnames = new String[5]
Stringnames [0] = "your phonenumber"
Stringnames[1] = "your email"
System.out.println(stringnames)
Or :
String[] Stringnames = new String[2]
String[] Stringnames = {"yournumber","your phonenumber"};
System.out.println(stringnames [1]);
String.split(...) is a java method for that.
EXAMPLE:
String content = "Ben Conan, General Manager, 90010021, benconan#gmail.com";
String[] selection = content.split(",");
System.out.println(selection[0]);
System.out.println(selection[3]);
BUT if you want to do a Regex then take a look at this:
https://stackoverflow.com/a/16053961/982161
Try this regex for phone number
[\d+]{8} ---> 8 represents number of digits in phone number
You can use
[\d+]{8,} ---> if you want the number of more than 8 digits
Use appropriate JAVA functions for matching. You can try the results here
http://regexr.com/
For email, it depends whether the format is simple or complicated. There is a good explanation here
http://www.regular-expressions.info/index.html
I have a string variable, I have to divide the content of the String variable into two parts and save them in two different string variables. I have already extracted one part of it, but I am not able to extract the other part.
This is the code:
String set_id="(1) Speed Test 150(min) Demo 1";
set_id = set_id.substring(set_id.indexOf("(") + 1);
set_id = set_id.substring(0, set_id.indexOf(")"));
The above code has extracted the digit 1 for me which is saved in the set_id variable.
Now I want to extract Speed Test 150(min) Demo 1 from the variable and save it in a variable named set_name.
The format of the variable's content will always remain the same, but the digit and the name itself may vary.
What should I do to extract the different parts of the string?
You are overwriting the original string when you are getting the first substring. Save each substring in a new variable:
String set_id="(1) Speed Test 150(min) Demo 1";
String part1 = set_id.substring(set_id.indexOf("(") + 1);
part1 = part1.substring(0, part1.indexOf(")"));
String part2 = set_id.substring(set_id.indexOf(")")+2);
Try the following:
\\((\d+)\\)\s*(.+)
$1 gives the id and $2 gives name.
Here,
\\( and \\) match opening and closing brackets. (escaped, as ( and ) have special meaning)
(\d+) matches one or more digits (captured, so that $1 can be used to refer this)
\s* matches zero or more spaces
(.+) matches one or more (any) characters (again captured)
Use it like
String string = "(1) Speed Test 150(min) Demo 1";
id = string.replaceAll("\\((\d+)\\)\s*(.+)","$1");
name = string.replaceAll("\\((\d+)\\)\s*(.+)","$2");
Assuming the format is the same:
set_id.substring(set_id.indexOf(")")+2);
Check this ... a better and efficient REGEX can be used ...
Pattern pattern = Pattern.compile("\\((\\d{0,1})\\)(.*$)");
String string = "(1) Speed Test 150(min) Demo 1";
Matcher matcher = pattern.matcher(string);
if (matcher.matches()) {
System.out.println("Total matches: " + matcher.groupCount());
for(int i=1, max=matcher.groupCount(); i<=max; i++ ) {
System.out.println(i + " : " + matcher.group(i));
}
} else {
System.out.println("No match");
}
I aaded "]" to find out out the end carachter.
String set_id="(1) Speed Test 150(min) Demo 1]";
String set_name=set_id.subSequence(set_id.indexOf(')')+1, set_id.indexOf(']')).toString();
System.out.println(set_name);
Now its working for me.
O/P:-- Speed Test 150(min) Demo
You can use Regex to extract the variables.Below is the sample code.
Pattern pattern = Pattern.compile("(\\d+).+?(\\d+)\\(");
String sample = "(1) Speed Test 150(min) Demo 1";
Matcher matcher = pattern.matcher(sample);
if(matcher.find()){
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
You could try this:
public class RegexTest {
public static void main(String[] args) {
String originalString = "(1) Speed Test 150(min) Demo 1";
String setId = originalString.replaceAll("\\((\\d+)\\)\\s*(.+)", "$1").trim();
String setName = originalString.replaceAll("\\((\\d+)\\)\\s*(.+)", "$2").trim();
System.out.println("setId: " + setId);
System.out.println("setName: " + setName);
}
}