Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a path /departments/{dept}/employees/{id}. How do I obtain dept and id from path /departments/{dept}/employees/{id}?
For example, I would like to obtain dept1 and id1 if path is /departments/dept1/employees/id1
I tried
String pattern1 = "departments/"
String pattern2 = "/employees"
Pattern p = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2));
Matcher m = p.matcher(text);
while (m.find()) {
String a = m.group(1);
}
Is there a simpler way to obtain dept1 and id1? I would prefer not using string.split as I have different paths for which I want to obtain path parameters and I prefer not having a dependency on the index position of the path parameters.
Using Spring... or:
String url = /departments/{dept}/employees/{id}
/----none--/-dept-/---none---/-id-
Make a split of url and get the position of array 1 and 3:
String urlSplited = url.split("/");
String dept = urlSplited[1];
String id = urlSplited[3];
If you are using Spring framework, then you can use a class specifically for this purpose named AntPathMatcher and use its method extractUriTemplateVariables
So, you can have the following:
AntPathMatcher matcher = new AntPathMatcher();
String url = "/departments/dept1/employees/id1";
String pattern = "/departments/{dept}/employees/{id}";
System.out.println(matcher.match(pattern, url));
System.out.println(matcher.extractUriTemplateVariables(pattern, url).get("dept"));
System.out.println(matcher.extractUriTemplateVariables(pattern, url).get("id"));
If you prefer regix:
import org.junit.Test;
import java.util.regex.Pattern;
public class ExampleUnitTest {
#Test
public void test_method() throws Exception {
Pattern digital_group = Pattern.compile("[//]");
CharSequence line = "test/message/one/thing";
String[] re = digital_group.split(line);
for (int i=0;i<re.length;i++) {
System.out.println(re[i]);
}
}
} //END: class ExampleUnitTest
The output is:
test
message
one
thing
Process finished with exit code 0
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
import string
def main():
inString =input("enter any number to decode : ")
message=" "
for numStr in string.split(instring):
asciiNum = eval(numStr)
message = message+chr(asciiNum)
print("\n", message)
main()
You need to convert the input to int by using int() function because the input is a string and ASCII is a int
To get the ASCII value, you can use chr()
So, for example:
inString = input("enter any number to decode : ")
number_to_decode = int(inString)
print( chr( number_to_decode ) )
It should work:
def main():
instring = input("enter any number to decode : ")
message = " "
for numStr in str.split(instring):
asciiNum = eval(numStr)
message = message + chr(asciiNum)
print("\n", message)
Here is a Java soln,
class AsciiConvertor{
public static void main(String[] args) {
int asciiVal = 87;
String strValue = Character.toString((char) asciiVal);
System.out.println(strValue);
}
}
Output: W
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how can I say that is my String match with this pattern: (install OS #name #version) which name and version can be any String without white space.
As you can use following Code:
// assuming parenthesis an sharp are pattern included:
String s = "(install OS #testname #testversion)";
if (s.matches("\\(install\\sOS\\s#\\S+\\s#\\S+\\)")) {
String[] splitted = s.split("\\s");
String name = splitted[2].replace("#", "");
String version = splitted[3].replace(")", "").replace("#", "");
System.out.println("name: " + name);
System.out.println("version: " + version);
}
// assuming parenthesis an sharp are both not pattern included:
s = "install OS testname2 testversion2";
if (s.matches("install\\sOS\\s\\S+\\s\\S+")) {
String[] splitted = s.split("\\s");
String name = splitted[2];
String version = splitted[3];
System.out.println("name: " + name);
System.out.println("version: " + version);
}
(if you need to allow mutliple whitespaces between parts, you have replace in both, split regex and match regex, \s with \s+)
This question already has answers here:
Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms
(10 answers)
Closed 3 years ago.
I have an XML request, the objective is to extract only the XML namespace.
<s:student xmlns:s="http://www.way2tutorial.com/some_url1"
xmlns:res="http://www.way2tutorial.com/some_url2">
<r:result>
<r:name>Opal Kole</r:name>
<r:sgpa>8.1</r:sgpa>
<r:cgpa>8.4</r:cgpa>
</r:result>
<res:cv>
<res:name>Opal Kole</res:name>
<res:cgpa>8.4</res:cgpa>
</res:cv>
</s:student>
I would not like to parse the XML as the ML parsing can be costly. But is there any way to get just the declared XML Namespaces
Expected Output:
xmlns:s="http://www.way2tutorial.com/some_url1"
xmlns:res="http://www.way2tutorial.com/some_url2"
I have even tried using regular expression, But it the expression was incorrect.
Java Code using regular expression:
String txt = "<s:student xmlns:s=\"http://www.way2tutorial.com/some_url1\" xmlns:res=\"http://www.way2tutorial.com/some_url2\">";
String regularExpression = "xmlns:(.*?)=(\".*?\")";
Pattern p = Pattern.compile(regularExpression);
Matcher m = p.matcher(txt);
if (m.find()) {
String word1 = m.group(1);
System.out.print("(" + word1.toString() + ")" + "\n");
}
If you literally just want:
xmlns:s="http://www.way2tutorial.com/some_url1"
xmlns:res="http://www.way2tutorial.com/some_url2"
Then you can use:
xmlns:[^=]+="[^"]+"
https://regex101.com/r/VwlSDN/4
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to extract parameters from a given url
I'm trying to retrieve just the numbers from the parameter in this url:
htt://tesing12/testds/fdsa?communityUuid=45352-32452-52
I have tried this with no luck:
^.*communityUuid=
Any help would be nice.
I recommend against the simple string-manipulation route. It's more verbose and more error prone. You may as well get a little help from the built-in classes and then use your knowledge that you're working with a URL (parameters delimited with "&") to guide your implementation:
String queryString = new URL("http://tesing12/testds/fdsa?communityUuid=45352-32452-52").getQuery();
String[] params = queryString.split("&");
String communityUuid = null;
for (String param : params) {
if (param.startsWith("communityUuid=")) {
communityUuid = param.substring(param.indexOf('=') + 1);
}
}
if (communityUuid != null) {
// do what you gotta do
}
This gives you the benefit of checking the well-formed-ness of the URL and avoids problems that can arise from similarly named parameters (the string-manipulation route will report the value of "abc_communityUuid" as well as "communityUuid").
A useful extension of this code is to build a map as you iterate over "params" and then query the map for any parameter name you want.
I don't see any reason to use a regex.
I would just do this:
String token = "communityUuid=";
String url = "htt://tesing12/testds/fdsa?communityUuid=45352-32452-52";
int index = url.indexOf(token) + token.length();
String theNumbers = url.substring(index);
NOTE:
You might have to look for the next parameter as well:
String token = "communityUuid=";
String url = "htt://tesing12/testds/fdsa?communityUuid=45352-32452-52";
int startIndex = url.indexOf(token) + token.length();
// here's where you might want to use a regex
String theNumbers = url.substring(startIndex).replaceAll("&.*$", "");