I'm trying to create a regex for a string I write down.
My string is like :
'AUR HALAA /PART="PROJECT" /ROLE="VR_ANALYST" /TYPE="C" /CAPABILITY="S" /ADD' (SUC)
The constant part in regex is :
'AUR
/ROLE=""
The inputs are:
HALAA
VR_ANALYST
I tried the regex like this:
\'(AUR) HALAA .* /ROLE="(.)" .
but it doesnt work.
Could you please show me some tricks to how to do this ?
Try this:
^AUR (\\w+).*?/ROLE="(\\w+)".*$
This regex might work for you
^AUR (\\w+) .*? /ROLE="(\\w+)" .*$
And, you can then use "groups" in Matcher class to get the matching groups which will give you HALAA at group(1) and VR_ANALYST at group(2)
Related
I have this Java code
String cookies = TextUtils.join(";", LoginActivity.msCookieManager.getCookieStore().getCookies());
Log.d("TheCookies", cookies);
Pattern csrf_pattern = Pattern.compile("csrf_cookie=(.+)(?=;)");
Matcher csrf_matcher = csrf_pattern.matcher(cookies);
while (csrf_matcher.find()) {
json.put("csrf_key", csrf_matcher.group(1));
Log.d("CSRF KEY", csrf_matcher.group(1));
}
The String contains something like this:
SessionID=sessiontest;csrf_cookie=e18d027da2fb95e888ebede711f1bc39;ci_session=3f4675b5b56bfd0ba4dae46249de0df7994ee21e
Im trying to get the csrf_cookie data by using this Regular Expression:
csrf_cookie=(.+)(?=;)
I expect a result like this in the code:
csrf_matcher.group(1);
e18d027da2fb95e888ebede711f1bc39
instead I get a:
3492f8670f4b09a6b3c3cbdfcc59e512;ci_session=8d823b309a361587fac5d67ad4706359b40d7bd0
What is the possible work around for this problem?
Here is a one-liner using String#replaceAll:
String input = "SessionID=sessiontest;csrf_cookie=e18d027da2fb95e888ebede711f1bc39;ci_session=3f4675b5b56bfd0ba4dae46249de0df7994ee21e";
String cookie = input.replaceAll(".*csrf_cookie=([^;]*).*", "$1");
System.out.println(cookie);
e18d027da2fb95e888ebede711f1bc39
Demo
Note: We could have used a formal regex pattern matcher, and in face you may want to do this if you need to do this search/replacement often in your code.
You are getting more data than expected because you are using an greedy '+' (It will match as long as it can)
For example the pattern a+ could match on aaa the following: a, aa, and aaa. Where the later is 'preferred' if the pattern is greedy.
So you are matching
csrf_cookie=e18d027da2fb95e888ebede711f1bc39;ci_session=3f4675b5b56bfd0ba4dae46249de0df7994ee21e;
as long as it ends with a ';'. The first ';' is skipped with .+ and the last ';' is found with the possitive lookahead
To make a patter ungreedy/lazy use +? instead of + (so a+? would match a (three times) on aaa string)
So try with:
csrf_cookie=(.+?);
or just match anything that is not a ';'
csrf_cookie=([^;]*);
that way you don't need to make it lazy.
My regular expression is of format "Exit* Order*". When i use in java its not working as expected.
String pattern = "Exit* Order*";
String ipLine = "Exiting orders";
Match: NO
String pattern = "Exit Order";
String ipLine = "Exit order";
Match: Yes.
Java Code:
Pattern patrn = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE);
Matcher match = patrn.matcher(ipLine);
Can any one let me know what should be the pattern in such cases.
I believe you are looking for something like:
"Exit.* Order.*"
or maybe something instead of .*, e.g. \S*, \w*, [A-Za-z]*.
Your current regular expression is looking for zero or more t and r on the ends of the words, e.g. it would match
Exi Orde
Exit Orde
Exitt Orde
Exi Order
Exi Orderr
...
Exit\\w* Order\\w*
You should use this..* can match much more than intended.use i or ignorecase flag
It seems like you just want to match "Exit Order" case-insensitively:
Try this:
if (str.matches("(?i)exit order"))
Or to restrict the match to just your examples, where the "O" of "Order may be "o", use:
if (str.matches("Exit [Oo]rder"))
Java does not use Linux regexp expression:
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Use this:
String pattern = "Exit.* Order.*";
I want a regular expression for a patterns which
1) String can contain atleast one '/' and one digit(/2/) or digits with spaces(//232 232/) or only one space(/// ////)
2) text is not allowed
**valid inputs:**
/1 323////
///////323 3232
//4343//4343
3/
**Invalid inputs:**
/////
121
///////3434dsds344//
//dsd///232
I have used ^/*(?:\\d[\\d ]*/*)*$ but this is failing for few of valid inputs like 232/////232
Can any one help ?
This one should work :
(?=.*\d)(?=.*\/)^[\d\/ ]+$
A simple alternation should suffice:
^(?:\d+ */+|/+ *\d+)[\d/ ]*$
I want to get content between two comments in some file.
like a file x
#user code
alert("");
alert("");
#user code
{
===
====
}
#user code
alert("as");
alert("as");
#user code
i am using this regex pattern to match
final Pattern pat = Pattern.compile("//#User code\r?\n(.*)\r?\n//#User code" , Pattern.DOTALL);
but its matching from first #user code to end of the file.
pls help.
A quick fix is to use .*? instead of just .*. The ? changes the * into a non-greedy repetition, which will match up until the nearest #user code, instead of the furthest.
The following regex works in the find dialog of Eclipse but throws an exception in Java.
I can't find why
(?<=(00|\\+))?[\\d]{1}[\\d]*
The syntax error is at runtime when executing:
Pattern.compile("(?<=(00|\\+))?[\\d]{1}[\\d]*")
In the find I used
(?<=(00|\+))?[\d]{1}[\d]*
I want to match phone numbers with or without the + or 00. But that is not the point because I get a Syntax error at position 13. I don't get the error if I get rid of the second "?"
Pattern.compile("(?<=(00|\\+))[\\d]{1}[\\d]*")
Please consider that instead of 1 sometime I need to use a greater number and anyway the question is about the syntax error
If your data looks like 00ddddd or +ddddd where d is digit you want to get #Bergi's regex (?<=00|\\+)\\d+ will do the trick. But if your data sometimes don't have any part that you want to ignore like ddddd then you probably should use group mechanism like
String[] data={"+123456","00123456","123456"};
Pattern p=Pattern.compile("(?:00|\\+)?(\\d+)");
Matcher m=null;
for (String s:data){
m=p.matcher(s);
if(m.find())
System.out.println(m.group(1));
}
output
123456
123456
123456
Here is an example that works for me:
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(?<=00|\\+)(\\d+)");
Matcher matcher = pattern.matcher("+1123456");
if (matcher.find()) {
System.out.println(matcher.group(1));
}
}
You might shorten your regex a lot. The character classes are not needed when there is only one class inside - just use \d. And {1} is quite useless as well. Also, you can use + for matching "one or more" (it's short for {1,}). Next the additional grouping in your lookbehind should not be needed.
And last, why is that lookbehind optional (with ?)? Just leave it away if you don't need it. This might even be the source of your pattern syntax error - a lookaround must not be optional.
Try this:
/(?<=00|\+)\d+/
Java:
"(?<=00|\\+)\\d+"