I try to parse log file:
'Del username /PART="deneme" /ROLE="VR_ANALYST" /TYPE="C" /CAPABILITY="S" /ADD' (S)
'Del Batu /PART="_PROJECT" /ROLE="VR_AUTHOR" /TYPE="R" /CAPABILITY="S" /ADD' (S)
RULE => 'Del input ........ /ROLE="input2" .......
input and input2 will given from user.
In this sentence :
username: should be a parameter (input from user)
VR_ANALYST: should be a parameter (input from user)
'Del: must be in the regex (the first four letters must be 'Del)
/ROLE="": must be in the regex
1)Regex should start with 'Del
2)continue with first input
3)some other strings
4)/ROLE="
5)second input
6)"
7)continue with other strings
I have nearly no knowledge about regex but I try to do :
'Del parameter \"*"\ (/ROLE=") parameter2 (") \*
Could you please give some advice how can create a regex of this sentence and use my paramters in this regex.
1.About regex i'll suggest You some of a online regex checker like (http://www.regexplanet.com/advanced/java/index.html) and just learn a little, its pretty simple ;)
2.If u want to add Your parameters to the regex u can use
replace(oldChar, newChar) on string that contains regex pattern.
Try this REGEX..
^'Del\s*parameter.*/ROLE="parameter2".*
Related
I have the following regular expression that I'm using to remove the dev. part of my URL.
String domain = "dev.mydomain.com";
System.out.println(domain.replaceAll(".*\\.(?=.*\\.)", ""));
Outputs: mydomain.com but this is giving me issues when the domains are in the vein of dev.mydomain.com.pe or dev.mydomain.com.uk in those cases I am getting only the .com.pe and .com.uk parts.
Is there a modifier I can use on my regex to make sure it only takes what is before the first . (dot included)?
Desired output:
dev.mydomain.com -> mydomain.com
stage.mydomain.com.pe -> mydomain.com.pe
test.mydomain.com.uk -> mydomain.com.uk
You may use
^[^.]+\.(?=.*\.)
See the regex demo and the regex graph:
Details
^ - start of string
[^.]+ - 1 or more chars other than dots
\. - a dot
(?=.*\.) - followed with any 0 or more chars other than line break chars as many as possible and then a ..
Java usage example:
String result = domain.replaceFirst("^[^.]+\\.(?=.*\\.)", "");
Following regex will work for you. It will find first part (if exists), captures rest of the string as 2nd matching group and replaces the string with 2nd matching group. .*? is non-greedy search that will match until it sees first dot character.
(.*?\.)?(.*\..*)
Regex Demo
sample code:
String domain = "dev.mydomain.com";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));
domain = "stage.mydomain.com.pe";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));
domain = "test.mydomain.com.uk";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));
domain = "mydomain.com";
System.out.println(domain.replaceAll("(.*?\\.)?(.*\\..*)", "$2"));
output:
mydomain.com
mydomain.com.pe
mydomain.com.uk
mydomain.com
I need to parse a a string which contains the query for the database.
The valid strings can be the following :
Status:OPEN,PENDING
Status:OPEN
Status:PENDING
Status:REJECTED
type:SMALL
type:BIG
weight>100
The following is not valid :
weight:100<PENDING
My first try was this in Java:
Pattern p = Pattern.compile("(\\w+?)(:|<|>)(\\w+)(|,)*(\\w+)*$");
Obviously, it fails to parse the last statement correctly.
I think that validating this with a regexpression is a hrad thing.
But you can try this expression:
"([\\w]+)(:(([A-Z]+)|([A-Z]+,([A-Z]+)))|(>[0-9]+))"
It worked for me in a online regexpression tester for your inputs
I created this expression for your requirement:
Pattern pattern = Pattern.compile("((Status:(OPEN|PENDING|OPEN,PENDING|REJECTED))|(type:(SMALL|BIG))|(weight[<>]\\d+))");
anything else matches invalid status, types, etc.
For example:
These are all invalid:
Status>100
type:REJECTED
weight:100
Hope this helps!
I got the following string to extract some information from:
String: String: String Number;
Right now I'm using the following regex to get the arguments:
(.*?):(.*?):(.*?);$
This way I would get with a Matcher the following output:
group(1) = String
group(2) = String
group(3) = String Number
If I want the number I need to execute another regex on the output of the 3rd group like the following:
([a-zA-Z]* ?([0-9])?$)
Used ont the String String Number this would give me and output like
group(1) = String
group(2) = Number
I thought about combining both steps and use a regex like (.*?):(.*?):([a-zA-Z]* ?([0-9])?);$ on the String: String: String Number;-String. But this does not work and I dont see the reason.
Hwere you go, I added some extra whitespace matching, but this seems to work, you were missing the whitespace between the second : and the following string
^(.*?):\s*(.*?):\s*([a-zA-Z]*\s+([0-9])?);$
I have the following strings:
<PAUL SAINT-KARL 1997-05-07>
<BOB DEAN 2001-05-07>
<GUY JEDDY 2007-05-07>
I want a java regex that would match this type of pattern "name and date" and then extract the name and date separately.
I able to match them separately with the following java regex:
1) (\d{4}-\d{2}-\d{2})>
2) <([ A-Z&#;0-9-]*+)
What I'm looking for is one regex that would identify the full text pattern as provided, and then extract the subsections, such as the actual name, and the date.
I'm looking to use Matcher.group() to retrieve the complete match from the target string.
Thanks
Try this:
"<([ A-Z&#;0-9-]*?) (\\d{4}-\\d{2}-\\d{2})>"
I changed the *+ to *? to make the * match lazily.
I want to split the string
String fields = "name[Employee Name], employeeno[Employee No], dob[Date of Birth], joindate[Date of Joining]";
to
name
employeeno
dob
joindate
I wrote the following java code for this but it is printing only name other matches are not printing.
String fields = "name[Employee Name], employeeno[Employee No], dob[Date of Birth], joindate[Date of Joining]";
Pattern pattern = Pattern.compile("\\[.+\\]+?,?\\s*" );
String[] split = pattern.split(fields);
for (String string : split) {
System.out.println(string);
}
What am I doing wrong here?
Thank you
This part:
\\[.+\\]
matches the first [, the .+ then gobbles up the entire string (if no line breaks are in the string) and then the \\] will match the last ].
You need to make the .+ reluctant by placing a ? after it:
Pattern pattern = Pattern.compile("\\[.+?\\]+?,?\\s*");
And shouldn't \\]+? just be \\] ?
The error is that you are matching greedily. You can change it to a non-greedy match:
Pattern.compile("\\[.+?\\],?\\s*")
^
There's an online regular expression tester at http://gskinner.com/RegExr/?2sa45 that will help you a lot when you try to understand regular expressions and how they are applied to a given input.
WOuld it be better to use Negated Character Classes to match the square brackets? \[(\w+\s)+\w+[^\]]\]
You could also see a good example how does using a negated character class work internally (without backtracking)?