How do I write this regex in Java? - java

Basically, for this regex
{(\(\(("\w{1,}",{0,1}){2}\),\(("[^:=;#"\)\(\{\}\[\]]{1,}",{0,1}){2}"[LR]{1}"\)\),{0,1}){1,}}
Which I've tested on Regexpal for this input:
{(("st0","sy0"),("st1","sy3","L")),(("st0","sy0"),("st1","^","L"))}
I now need in Java. I can't seem to figure out how to convert it. Can somebody show me how to?

You need to escape the special chars - specifically the backslashes and the quote marks.

The regular expression could work on Java, the only thing that you have to do, is escape the backslash .

Related

Why do I need two slashes in Java Regex to find a "+" symbol?

Just something I don't understand the full meaning behind. I understand that I need to escape any special meaning characters if I want to find them using regex. And I also read somewhere that you need to escape the backslash in Java if it's inside a String literal. My question though is if I "escape" the backslash, doesn't it lose its meaning? So then it wouldn't be able to escape the following plus symbol?
Throws an error (but shouldn't it work since that's how you escape those special characters?):
replaceAll("\+\s", ""));
Works:
replaceAll("\\+\\s", ""));
Hopefully that makes sense. I'm just trying to understand the functionality behind why I need those extra slashes when the regex tutorials I've read don't mention them. And things like "\+" should find the plus symbol.
There are two "escapings" going on here. The first backslash is to escape the second backslash for the Java language, to create an actual backslash character. The backslash character is what escapes the + or the s for interpretation by the regular expression engine. That's why you need two backslashes -- one for Java, one for the regular expression engine. With only one backslash, Java reports \s and \+ as illegal escape characters -- not for regular expressions, but for an actual character in the Java language.
Funda behind extra slashes is that , first slash '\' is escape for the string and second slash '\' is escape for the regex.

why does this regex not work?

I am trying to match a string with a java regex and I cannot succeed. I'm pretty new to java and with most of my experience being linux based regex, I've had no success. Can someone help me?
Below are the codes that Im using.
The regex is-
//vod//final\_\d{0,99}.\d{0,99}\\-Frag\d{0,99}
The line that I'm trying to match is
/vod/final_1.3Seg1-Frag1
where I want 1.3, 1 and 1 to be wildcarded.
Someone please help me out... :(
You are missing the Seg1 part. Also you are escaping characters that need not to be escaped. Try out this regexp: /vod/final_\\d+\\.\\d+Seg1-Frag\\d+
This should work:
Pattern p = Pattern.compile( "/vod/final_\\d+\\.\\d+Seg\\d+-Frag\\d+" );
Notes: To protect special characters, you can use Pattern.quote()
When running into problems like this, start with a simple text and pattern and build from there. I.e. first try to match /, then /vod/, then /vod/final_1, etc.
You're escaping too much. Don't escape /, _, -.
Something like:
/vod/final_\d{0,99}.\d{0,99}-Frag\d{0,99}
Does this work?
/\/vod\/final\_\d{0,99}.\d{0,99}Seg\d-Frag\d{0,99}
Also, here's what I used to edit the regex you provided above: http://rubular.com/
It says it's for ruby, but it also mentions that it works for java too.

How to use regex to remove punctuations in a sentence

I am trying to take from a file all the valid words. Valid words are defined as normal characters that can appear like so:
don't won't can't
and I have to ignore commas periods and exclamation points.
I have gotten the expression to just get characters but now it won't get words like don't and can't or won't.
This is the expression I am using "[^A-Za-z]+" and I have tried "\'[^A-Za-z]+" but this breaks and allows all characters. Does anyone have any idea what I can use to get normal words including don't and won't and can't and such words.
Thank you very much
[^A-Za-z] Would mean anything NOT matching those character ranges! Try this:
[A-Za-z']
You may need to escape the single quote, in which case you'll probably need to escape the slash that escapes it:
[A-Za-z\\']
Another way (using abbreviations) is: \b[\w']+
This will match letters from any language and exclude numbers.
\b[\p{L}\!\'\?]+
Here is a very good resource for regular expressions.
http://www.regular-expressions.info/

How to escape special characters in the regex ***(.*)

I am new to Java. Can somebody help me?
Is there any method available in Java which escapes the special characters in the below regex automatically?
Before escaping ***(.*) and after escaping \\*\\*\\*(.*)
I don't want to escape (.*) here.
On the face of it, Pattern.quote appears to do the job.
However, looking at the detail of your question, it appears that you want / expect to be able to escape some meta-characters and not others. Pattern.quote won't do that if you apply it to a single string. Rather, it will quote each and every character. (For the record, it doesn't use backslashes. It uses "\E" and "\Q".\ which neatly avoids the cost of parsing the string to find characters that need escaping.)
But the real problem is that you haven't said how the quoter should decide which meta-characters to escape and which ones to leave intact. For instance, how does it know to escape the first three '' characters, but not the "."?
Without a clearer specification, your question is pretty much unanswerable. And even with a specification, there is little chance of finding an easy way to do this.
IMO, a better approach would be to do the escaping before you assemble the pattern from its component parts ... assuming that's what is going on here.

Java Regex - Checking for Carriage Return

I am writing a regex to capture SQL Injection Keywords on the HTTP request.
I am having problems with allowing 0 or more carriage returns.
I have tried \s but I get the error 'errorInvalidEscaperChar'.
If I put \s then this will be taken as a literal \s.
Any ideas please?
Thanks
N
You'll have to escape the \ in your string. try "\\s".
You have to escape the backslash in Java Strings. So, to get a backslash followed by an s, you need to write: "\\s".
But let's take a step back, what are you trying to achieve? Searching in parameters for "SQL Injection Keywords" might be a bad idea. Could you provide some details?
"\" is a citation character in regexp and Java. So you need to write \\s

Categories

Resources