String manipulation using Java regexp - java

I have a String in this format:
mydb://<user>:<password>#<host>:27017
And I would like to use Java regexp in order to extract the <user> and <password> strings from the String. What would be the best way doing so?
EDIT:
I would like to be able to use this regexp in the String's replace method so that I'm left only with the relevant user and password Strings

You can use this regex (Pattern)
Pattern p = Pattern.compile("^mydb://([^:]+):([^#]+)#[^:]+:\\d+$");
And then capture group #1 and #2 will have your user and password respectively.
Code:
String str = "mydb://foo:bar#localhost:27017";
Pattern p = Pattern.compile("^mydb://([^:]+):([^#]+)#[^:]+:\\d+$");
Matcher matcher = p.matcher(str);
if (matcher.find())
System.out.println("User: " + matcher.group(1) + ", Password: "
+ matcher.group(2));
OUTPUT:
User: foo, Password: bar
EDIT: Based on your comments: if you want to use String methods then:
String regex = "^mydb://([^:]+):([^#]+)#[^:]+:\\d+$";
String user = str.replaceAll(regex, "$1");
String pass = str.replaceAll(regex, "$2")

Related

How can i replace this?

How can I replace this
String str = "KMMH12DE1433";
String pattern = "^[a-z]{2}([0-9]{2})[a-z]{1,2}([0-9]{4})$";
String str2 = str.replaceAll(pattern, "repl");
Log.e("Founded_words2",str2);
What I got: KMMH12DE1433
What I want: MH12DE1433
Try it like this using a proper java.util.regex.Pattern and a java.util.regex.Matcher:
String str = "KMMH12DE1433";
//Make the pattern, case-insensitive using (?i)
Pattern pattern = Pattern.compile("(?i)[a-z]{2}([0-9]{2})[a-z]{1,2}([0-9]{4})");
//Create the Matcher
Matcher m = pattern.matcher(str);
//Check if we find anything
if(m.find()) {
//Use what you found - with proper capturing groups you
//gain access to parts of your pattern as needed
System.out.println("Found this: " + m.group());
}
If you just want to remove the first two characters and if the first two characters will always be uppercase letters:
String str = "KMMH12DE1433";
String pattern = "^[A-Z]{2}";
String str2 = str.replaceAll(pattern, "");
Log.e("Output string: ", str2);
try this :
String a = "KMMH12DE1433";
String pattern = "^[A-Z]{2}";
String rs = a.replaceAll(pattern,"");
Please change like this
String ans=str.substring(0);

Regular expression on a string

I have a String like below
String phone = (123) 456-7890
Now I would like my program to verify if that my input is the same pattern as string 'phone'
I did the following
if(phone.contains("([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]")) {
//display pass
}
else {
//display fail
}
It didn't work. I tried with other combinations too. nothing worked.
Question :
1. How can I achieve this without using 'Pattern' like above?
2. How to do this with pattern. I tried with pattern as below
Pattern pattern = Pattern.compile("(\d+)");
Matcher match = pattern.matcher(phone);
if (match.find()) {
//Displaypass
}
String#matches checks if a string matches a pattern:
if (phone.matches("\\(\\d{3}\\) \\d{3}-\\d{4}")) {
//Displaypass
}
The pattern is a regular expression. Therefor I had to escape the round brackets, as they have a special meaning in regex (they denote capturing groups).
contains() only checks if a string contains the substring passed to it.
I'm not going to dive too deeply into regex syntax, but there definitely is something off with your regex.
"([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]"
it containes ( and ) and those have special meaning. Escape them
"\([0-9][0-9][0-9]\) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]"
and you'll also have to escape your \ for the final
"\\([0-9][0-9][0-9]\\) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]"
You can write like:
Pattern pattern = Pattern.compile("\\(\\d{3}\\) \\d{3}-\\d{4}");
Matcher matcher = pattern.matcher(sPhoneNumber);
if (matcher.matches()) {
System.out.println("Phone Number Valid");
}
For more information you can visit this article.
It appears that your problem is that you didn't escape the parentheses, so your Regex is failing. Try this:
\([0-9][0-9][0-9]\) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]
This works
String PHONE_REGEX = "[(]\\b[0-9]{3}\\b[)][ ]\\b[0-9]{3}\\b[-]\\b[0-9]{4}\\b";
String phone1 = "(1234) 891-6762";
Boolean b = phone1.matches(PHONE_REGEX);
System.out.println("is e-mail: " + phone1 + " :Valid = " + b);
String phone2 = "(143) 456-7890";
b = phone2.matches(PHONE_REGEX);
System.out.println("is e-mail: " + phone2 + " :Valid = " + b);
Output:
is phone: (1234) 891-6762 :Valid = false
is phone: (143) 456-7890 :Valid = true

How to use pattern in Java to fetch groups like 'sscanf' does in C?

I have String user#domain:port
I want to fetch user, domain and port from this String.
So I created regex:
public static final String MATCH_USER_DOMAIN_PORT = "^([0-9,a-zA-Z-.*_]+)#([a-z0-9]+[\\.-][a-z0-9]+\\.[a-z]{2,}+):(6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[1-5]\\d{4}|[1-9]\\d{0,3})$";
and this is my method in Unitest so far:
public void test____matchesUserDomainWithPort(){
String identityText = "maxim#domain.com:5555";
String user = "";
String domain = "";
String port = "";
if(identityText.matches(MATCH_USER_DOMAIN_PORT))
{
Pattern p = Pattern.compile(MATCH_USER_DOMAIN_PORT);
Matcher m = p.matcher(identityText);
user = m.group(1);
domain= m.group(2);
port= m.group(3);
}
assertEquals("maxim", user);
assertEquals("domain.com", domain);
assertEquals("5555", port);
}
I get error:
java.lang.IllegalStateException: No successful match so far
at java.util.regex.Matcher.ensureMatch(Matcher.java:607)
....
in row: user = m.group(1);
I opened http://gskinner.com/RegExr/?2v5r0
and there all seems good:
Output:
RegExp: /^([0-9,a-zA-Z-.*_]+#[a-z0-9]+([\.-][a-z0-9]+)*)+\.[a-z]{2,}+:(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3})$/
pattern: ^([0-9,a-zA-Z-.*_]+#[a-z0-9]+([\.-][a-z0-9]+)*)+\.[a-z]{2,}+:(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3})$
flags:
3 capturing groups:
group 1: ([0-9,a-zA-Z-.*_]+#[a-z0-9]+([\.-][a-z0-9]+)*)
group 2: ([\.-][a-z0-9]+)
group 3: (6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3})
Do I miss something?
in C i just write: sscanf(identityText,"%[^#]#%[^:]:%511s",user,domain,port);
For sure I can split this text with # and : and get 3 values, but its interesting how to do that in gentle form :)
Please, help
Please use
if(identityText.matches(MATCH_USER_DOMAIN_PORT)){
Pattern p = Pattern.compile(MATCH_USER_DOMAIN_PORT);
Matcher m = p.matcher(identityText);
while(m.find()){
user = m.group(1);
domain= m.group(2);
port= m.group(3);
}
}
thanks
Yes, I think your regex is wrong.
public static final String MATCH_USER_DOMAIN_PORT = "^([0-9,a-zA-Z-.*_]+#[a-z0-9]+([\\.-][a-z0-9]+)*)+\\.[a-z]{2,}+:(6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[1-5]\\d{4}|[1-9]\\d{0,3})$";
To break it down:
^(
[0-9,a-zA-Z-.*_]+
any number of these characters, will match "maxim"
#
will match "#"
[a-z0-9]+
any number of these characters, will match "domain"
([\\.-][a-z0-9]+)*
will match ".com" (or theoretically ".somethingelse.com", nice)
)+
will make group #2 "maxim#domain.com", I believe, but what's with the "+" ?
\\.
nothing in the input string here
[a-z]{2,}+
is this for a country code like .eu ? Again, what's with the "+" ?
:
(6553[0-5]|655[0-2]\\d|65[0-4]\\d{2}|6[0-4]\\d{3}|[1-5]\\d{4}|[1-9]\\d{0,3})
seems overly complicated - probably don't do the numeric validation with the regex
$
Take a look at Using a regular expression to validate an email address for some advice on validation of email addresses.

Break a string variable's content into two parts

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);
}
}

How to filter a String in java with specific range (email format)

I have a String in Java called Kiran<kiran#gmail.com>. I want to get String just kiran#gmail.com by removing other content.
String s1= kiran<kiran#gmail.com>
Output should be kiran#gmail.com
Please help me out in solving this.
If you are trying to parse email addresses, I'd recommend to use the InternetAddress class. It is part of Java EE (if you are using Java SE you need to add the javax.mail dependency).
That class is able to parse an String containing an email address like yours.
String s1 = "kiran<kiran#gmail.com>";
InternetAddress address = new InternetAddress(s1);
String email = address.getAddress();
I think this way:
Your algorithm is automatically standards-compliant
Your code is cleaner than using regular expressions to extract the email address.
You can do the following thing.
String s = "To: John Smith <john#smith.com>, Janes Smith\n"
+ "<jane#smith.org>, Tom Barter <tom#test.co.uk>, Other \n"
+ "Weird ##$#<>#^Names <other#names.me>, \n"
+ "Long Long Long Long Name <longlong#name.com>";
s = s.substring(3); // filter TO:
System.out.println(s);
// Use DOTALL pattern
Pattern p = Pattern.compile("(.*?)<([^>]+)>\\s*,?",Pattern.DOTALL);
Matcher m = p.matcher(s);
while(m.find()) {
// filter newline
String name = m.group(1).replaceAll("[\\n\\r]+", "");
String email = m.group(2).replaceAll("[\\n\\r]+", "");
System.out.println(name + " -> " + email);
}

Categories