If I have a string "Sc_bookstore.PKG_book.SP_Harrypotter", for example.
How would I separate the schema, package and StoredProc? i.e, I want to get he following output:
Sc_bookstore
PKG_book
SP_Harrypotter
I am going to be using this regex on multiple StoredProc calls, and all of them follow the standard of "Sc_" "PKG_" "SP_"
Thanks in advance!
Use "\\." or "[.]" if a regular expression split delimiter is required (as with String.split).
A . in a Java regular expression means "match any character" (roughly, depending on options); the two forms above escape/prevent this meaning so it matches a literal period.
Any time you need to divide a string by a delimeter, you should consider using the String split method:
String s = "Sc_bookstore.PKG_book.SP_Harrypotter";
String parts[] = s.split("\\.");
parts[0]; // holds "Sc_bookstore"
parts[1]; // holds "PKG_book"
parts[2]; // holds "SP_Harrypotter"
If you really want/need Regex, something in the lines of:
^(Sc_\w+)\.(PKG_\w+)\.(SP_\w+)$
Would allow you to capture the groups values
For input Sc_bookstore.PKG_book.SP_Harrypotter:
Group 1: "Sc_bookstore"
Group 2: "PKG_book"
Group 3: "SP_Harrypotter"
But, in your case, just splitting the String by . would suit your needs very well
you may use split (with no regex)
String[] values;
values = str.split("\\.");
Assuming that a single string always contains exactly one such record, use this:
^Sc_([^.]+)\.PKG_([^.]+)\.SP_([^$]+)$
...note that this removes the Sc_, PKG_ and SP_ parts of the string and only returns "content".
For testing purposes, I created a simple JavaRegexTester application to test regular expressions.
Related
I'm filtering out string using below regex
^(?!.*(P1 | P2)).*groupName.*$
Here group name is specific string which I replace at run time. This regex is already running fine.
I've two input strings which needs to pass through from this regex. Can't change ^(?!.*(P1 | P2)) part of regex, so would like to change regex after this part only. Its a very generic regex which is being used at so many places, so I have only place to have changes is groupName part of regex. Is there any way where only 2 string could pass through this regex ?
1) ADMIN-P3-UI-READ-ONLY
2) ADMIN-P3-READ-ONLY
In regex groupName is a just a variable which will be replaced at run time with required string. In this case I want 2 string to be passed, so groupName part can be replaced with READ-ONLY but it will pass 1 string too.
Can anyone suggest on this how to make this work ?
You could use negative lookBehind:
(?<!UI-)READ-ONLY
so there must be no UI- before READ-ONLY
You can add another lookahead at the very start of your pattern to further restrict what it matches because your pattern is of the "match-everything-but" type.
So, it may look like
String extraCondition = "^(?!.*UI)";
String regex = "^(?!.*(P1|P2)).*READ-ONLY.*$";
String finalRegex = extraCondition + regex;
The pattern will look like
^(?!.*UI)^(?!.*(P1|P2)).*READ-ONLY.*$
matching
^(?!.*UI) - no UI after any zero or more chars other than line break chars as many as possible from the start of string
^(?!.*(P1|P2)) - no P1 nor P2 after any zero or more chars other than line break chars as many as possible from the start of string
.*READ-ONLY - any zero or more chars other than line break chars as many as possible and then READ-ONLY
.*$ - the rest of the string. Note you may safely remove $ here unless you want to make sure there are no extra lines in the input string.
I want to match a string which occurs after a certain pattern but I am not able to come up with a regex to do that (I am using Java).
For example, let's say I have this string,
caa,abb,ksmf,fsksf,fkfs,admkf
and I want my regex to match only those commas which are prefixed by abb. How do I do that? Is it even possible using regexes?
If I use the regex abb, it matches the whole string abb, but I only want to match the comma after that.
I ask this because I wanted to use this regex in a split method which accepts a regex. If I pass abb, as the regex, it will consider the string abb, to be the delimiter and not the , which I want.
Any help would be greatly appreciated.
String test = "caa,abb,ksmf,fsksf,fkfs,admkf";
String regex = "(?<=abb),";
String[] split = test.split(regex);
for(String s : split){
System.out.println(s);
}
Output:
caa,abb
ksmf,fsksf,fkfs,admkf
See here for information:
https://www.regular-expressions.info/lookaround.html
Sample Input:
a:b
a.in:b
asds.sdsd:b
a:b___a.sds:bc___ab:bd
Sample Output:
a:replaced
a.in:replaced
asds.sdsd:replaced
a:replaced___a.sds:replaced___ab:replaced
String which comes after : should be replaced with custom function.
I have done the same without Regex. I feel it can be replaced with regex as we are trying to extract string out of specific pattern.
For first three cases, it's simple enough to extract String after :, but I couldn't find a way to deal with third case, unless I split the string ___ and apply the approach for first type of pattern and again concatenate them.
Just replace only the letters with exists next to : with the string replaced.
string.replaceAll("(?<=:)[A-Za-z]+", "replaced");
DEMO
or
If you also want to deal with digits, then add \d inside the char class.
string.replaceAll("(?<=:)[A-Za-z\\d]+", "replaced");
(:)[a-zA-Z]+
You can simply do this with string.replaceAll.Replace by $1replaced.See demo.
https://regex101.com/r/fX3oF6/18
I have a specific requirement to find a pattern and replace the value of matching group(2) in the original string by retaining the pattern(delimiter), I am using the pattern
:(\w+)[:\|]+(.*)
With this pattern it parse the values correctly but i am not able to replace the value of group(2). For example i have a multi-line input string
:20:9405601140
:2D::11298666
:28C:20/1
I want to replace the value(9405601140) of tag 20 with new value(1234) so the output i am expecting is
:20:1234
:2D::11298666
:28C:20/1
Thanks
Use this one:
input = input.replaceAll("(:20):(\\d+)(?!\\d)", "$1:1234");
Here (\\d+)(?!\\d) is checking whether the digits after the :20: are not followed by a digit or not.
However, if you want to replace only the :20:9405601140 there here it is much simple:
input = input.replaceAll(":20:9405601140(?!\\d)", ":20:1234");
You can do this by capturing what you want to keep, instead of what you want to replace, and then using a backreference ($1, for the first capturing group) in the replacement string to include it in the final result.
Something like:
string.replaceAll("(:\\w+[:\\|]+).*", "$11234")
To perform the replacement on all the given lines, or just:
string.replaceAll("(:20[:\\|]+).*", "$11234")
To perform the replacement only on the line beginning with ":20".
try this
s = s.replaceAll("\\A(?::[:\\|])\\w+", "1234");
How about doing it the other way around.
Create a pattern like this (:(\w+)[:\|]+)(.*) then for each row output the first group and your replacement (instead of group 2).
Here is an working example http://ideone.com/9TkGx6
I am new to java, i have a string
"rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23"
What I want is to get the Driving Test word. The word is dynamically changes so what I want to happen is get the word between the rdl_mod_name: and the \n.
Try the following.. It will work in your case..
String str = "rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23";
Pattern pattern = Pattern.compile("rdl_mod_name:(.*?)\n");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Also you can make use of regex,matcher,pattern to get your desired result..
The following links will also give you a fair idea:
Extract string between two strings in java
Java- Extract part of a string between two special characters
How to get a string between two characters?
I would look into java regular expressions (regex). The String matches method uses a regex to determine if there's a pattern in a string. For what you are doing, I would probably use 'matches(rdl_mod_.*\n)'. The '.*' is a wildcard for strings, so in this context it means anything between rdl_mod and \n. I'm not sure if the matches method can process forward slashes (they signify special text characters), so you might have to replace them with either a different character or remove them altogether.
Use java's substring() function with java indexof() function.
Try this code :
String s = "rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23";
String sArr[] = s.split("\n\n");
String[] sArr1 = sArr[1].split(":");
System.out.println("sArr1[1] : " + sArr1[1]);
The s.split("\n\n");will split the string on basis of \n\n.
The second split i.e. sArr[1].split(":"); will split the second element in array sArr on basis of : i.e split rdl_mod_name:Driving Test into rdl_mod_name and Driving Test.
sArr1[1] is your desired result.