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 9 years ago.
Improve this question
A friend had an interview question asking the following:
Given a string comprising of the characters (,),{,},[,], determine if is well formed or not.
In my mind I would have answered no as it is a string and so "/" character would be required to print said characters. Is this correct or am I way off base?
if well-formed means each brace is closed with a matching brace and there are no incidents like ({)}, then I would suggest you to use stack
go through each character in the string
if it is opening brace, push it on the stack
if it is closing brace, pop from stack and look, if it is a match
-> if you go through all chars in string and stack is empty, your string is well formed
You seem to be asking what the interview question means.
The answer is that is means what the interviewer meant it to mean. You (or your friend) should ask the interviewer for clarification if you need it. (Indeed, if you didn't ask the interviewer for clarification, you might "lose points" ... for not asking.)
However, a reasonable interpretation would be that the question is asking the interviewee to write a method to check that a String consisting of those characters has balanced bracketing; e.g.
[]{[]} OK
[ BAD
[(]) BAD
FWIW - there is no general definition for a "well-formed string". Rather a string is considered to be well-formed with respect to some grammar, if a valid parse tree can be constructed for the string using the productions of the grammar.
The problem in this case is that no such grammar has been provided (at least, not here). Hence the interview question (as stated) is incomplete / ambiguous / only answerable if you are prepared to guess what grammar the interviewer means.
The correct answer is "exactly what do you mean by well formed?" Vague requirements should be made more specific, and that's a signal that you are looking for correct functionality, not a quick fix.
We can guess as to what it means. It might mean correct nesting of elements, such that opening and closing parenthesis follow an xml-style tag syntax, where
{([)]}
would not be well formed
It might mean properly escaped so that it can be printed in an environment where the characters are interpreted as commands. In such a case, well formed might mean
\{\(\[
It might even mean that once an opening character is found, the only following legal character is its closing character. In such a case, the following would not be well formed.
{()}
If well formed just means one closing item for each opening item, then (due to the definition) the following even might be ok
)(
In short, when you have doubt, ask. Asking is the key to so many things, and it shows that you are paying attention and thinking. These skills are far more important than "knowing the answer" because knowing the answer without understanding the request results in answering the wrong question.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this regex which is a bit longer than usual. I try to capture some values in a text document.
\\n*.*(k\\s=\\s\\d)(.|\\n)*?estimate\\s.*\\n*\\s*((\\d+|<)\\.\\d+)\\s*((\\d+|<)\\.\\d+)\\s*((\\d+|<)\\.\\d+)\\s*((\\d+|<)\\.\\d+)\\s*((\\d+|<)\\.\\d+)\\s*((\\d+|<)\\.\\d+)\\s+
It works perfectly fine on regexr.com link
but in Java only this part works
\\n*.*(k\\s=\\s\\d)(.|\\n)*?estimat
as soon as I add the missing 'e' it stops working.
For now I am ignoring that some groups are filled wrongly.
What goes wrong?
The (.|\\n)*? makes the regex engine perform too many redundant backtracking steps. You need to replace all such parts in your pattern with (?s:.*?), a modifier group that matches any 0+ chars including line break chars. Since there is no alternation, there is no redundant backtracking here.
Note that in JavaScript (as you are testing the pattern at regexr.com that only supports JavaScript regex flavor), the (.|\n)*? should be replaced with [^]*? or [\s\S]*? as its regex engine does not support inline modifiers at all.
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 6 years ago.
Improve this question
I have CSV file which contains following line.
INPUT:
No,NAme,ID,Description
1,Stack,232,"ABCDEFGHIJKLMNO
-- Jiuaslkm asdasdasd"
2,Queue,454,"PQRSTUVWXYZ
-- Other
words here"
3,Que,4343,"sdfwerrew"
OUTPUT EXPECTED:
No,NAme,ID,Description
1,Stack,232,"ABCDEFGHIJKLMNO \n -- Jiuaslkm asdasdasd"
2,Queue,454,"PQRSTUVWXYZ \n -- Other \n words here"
3,Que,4343,"sdfwerrew"
or
No,NAme,ID,Description
1,Stack,232,"ABCDEFGHIJKLMNO -- Jiuaslkm asdasdasd"
2,Queue,454,"PQRSTUVWXYZ -- Other words here"
3,Que,4343,"sdfwerrew"
Is there any java regex pattern available to find and merge the lines based starting double quotes and end quotes?
You are going down the wrong path. Not everything should be solved using regular expressions. CSV parsing is one of those things.
Seriously: you are about to re-invent the wheel. And the wheel you are about to create will be deficient, and prone to break over and over again.
The sane approach: there are many existing CSV parsers for Java out there. They deal perfectly with multi-line values. So: use one of them (see here as starting point for the many choices you have)
There is a nice rule of thumb: when your regex becomes so complicated that you can't write it down yourself; then consider doing things differently. You are the person who owns this code; you will have to maintain and maybe enhance it - not those folks here that are able to write down a regex that solves this one flavor of CSV example input.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to use java's string.replaceAll() or replaceFirst() method in order to edit data read from a pdf document. A line of data that could be returned is:
21/1**E (6-11) 4479 77000327633 (U)
I wish to only store the 77000327633 into a variable for working with and looking for the correct regex that will capture ONLY this 11 digit number. I've tried searching around for a regex but nothing seems to give me my desired outcome.
It could be done like this:
String value = "21/1**E (6-11) 4479 77000327633 (U)";
Pattern pattern = Pattern.compile(".* (\\d{11}) .*");
System.out.println(pattern.matcher(value).replaceAll("$1"));
Output:
77000327633
NB: This assumes that your number has 11 digits and that there is a space before and after.
NB2: It is not meant to be perfect it is only to show the idea which is here to define a global pattern with a group and replace everything by the content of the group
This is it : (.*)[ ]([0-9])*[ ](.*)
Can access to your value using $2
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
System.out.println("Strings to be printed");
In the above line of code, the strings are coated with double inverted commas
and when we want to print the non-string values or variables, we separate by commas.
Does the compiler ask for the commas? or is it required by the println(); to determine the strings separately?
why is inverted commas required by println();?
It is not so much required by println as required by the Java language.
The println method takes a String argument, and the way to write a String literal in Java is you put double-quote characters around it.
If you didn't put the double-quotes around 'String to be printed' then the Java Language Specification says that you have a sequence of 4 identifiers: String to be printed. That is not a valid Java expression ... and you will get a compilation error.
Why is it that way? Well, it is also consistent with the vast majority of other programming languages ... including Java's direct antecedents ... and that is as good an explanation as you are likely to get.
I suppose you could also be asking why they need to have rules like this: why couldn't the compiler just figure out what the programmer means. And the answer to that is that it is beyond the state of the art ... and probably beyond the realms of possibility. (How does a compiler figure out what the programmer means, when a lot of the time the programmer doesn't know himself!?)
Java: Creating Strings
The most direct way to create a string is to write:
String greeting = "Hello world!";
In this case, "Hello world!" is a string literal—a series of
characters in your code that is enclosed in double quotes.
Whenever it encounters a string literal in your code, the compiler
creates a String object with its value—in this case, Hello world!.
So it's not the requirement of println() but it is a way to create a string literal in Java.
"Strings to be printed"
This is called a String Literal. The compiler understands it as a string. If you want to add a " in the middle of it you use a \ before it to escape the character.
"Strings \"to\" be printed"
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 8 years ago.
Improve this question
I am writing a simplified Java compiler. I wrote a regex for variable name:
"(_?[a-zA-Z]+[\w]*)"
and I want to add that the name can not be certain words, like int, double, true, false...
I tryed using ^ , but it is not working.
It can be done with a RE, but it's not easy for a human to write it. Treat keywords as identifiers in the scanner and distinguish the identifiers vs keywords in the tokenizer afterwards. That should be substantially easier.
I don't believe that this should do that via regular expressions but rather can be better done using a HashSet<String> and exclude identifier names that are contained in the set.
^ is used for something else :
^ may appear at the beginning of a pattern to require the match to
occur at the very beginning of a line. For example, ^abc matches
abc123 but not 123abc.
consider using "(?!...)" :
(?!...) is a negative look-ahead because it requires that the
specified pattern not exist.
i suggest that if it's impossible or too hard , go to real coding instead . sometimes , regular expressions can be much slower than real , optimized code , and they can be very confusing and you might have problems finding what's wrong with what you've written.
for trying out your regular expressions , check this one:
http://gskinner.com/RegExr/
for quick referencing , check this one:
http://www.autohotkey.com/docs/misc/RegEx-QuickRef.htm