Is there a way to append string to the end of lines in Eclipse?
Search and find seems like it would work, but using find with just the regex expression $ does not find any strings. .$ will find something, but running find replace with this deletes the last character of your line, which is undesirable. Does anyone know a way to accomplish this in Eclipse? Is there something I am doing wrong with my regex that might make Eclipse not understand this, while other editors like vim handle it just fine.. (in Vi / Vim :0,$s/$/appended to end of line/).
Surely I am not the only person who wishes there was this functionality... It's offered by most other good editors. Could this be considered a bug?
I agree that this is a bug in eclipse. I tried the same as you with the same results. I also tried to use the regex search string "(?<=.)$" to try to ignore the single character match in the replace but that failed as well. One should be able to search for end of string to append.
Here's a trick to make it work,
Find: "(.)$"
Replace: $1foo
This replaces the single character match before the end of line and appends foo.
That's a lot of hoop jumping but at least it works.
I'm wondering if the best bet would be to run a Java program on the list of variables before you copy them in. I'm not sure of the format of the file which you have cut and paste from but if it is just a list with only the variable names on each line, try this:
ArrayList<String> importarray = new ArrayList<String>();
ArrayList<String> rebuildarray = new ArrayList<String>();
BufferedReader bufferedfile = new BufferedReader();
public static void main(String[] args) {
readFile();
processFile();
}
static void readFile() {
String file = "C:\\path\\file.txt";
try {
String line;
importstart = new BufferedReader(new FileReader(file));
for (line = importstart.readLine(); line != null; line = importstart.readLine()) {
importarray.add (line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static void processFile() {
String complete = "";
for (String string : importarray) {
complete = string + "\";";
rebuildarray.add(complete);
}
}
Adding this in would provide an array of variable names with " "; " on the end.
Alternatively, you could use this array in the String declaration and do:
for (String variable : rebuildarray) {
final String string = variable;
doSomething(string);
}
This would negate the need for the addition of ";.
Note sure if this is a bit too much, or even entirely what you were looking for, but they are a couple of ideas.
In my case, using Eclipse Luna (4.4.0), the accepted solution didn't work. It is only replacing the first line and leaving the others. But the worked (wanted to added a semi-colon):
find: ^.*$
Replace: $0;
Related
I want to break a string on . (full stop).
Ex. String str="We are going there.How are you."
then output should be
We are going there.
How are you.
It should split on "."
But if my string is
Dr.Harry is going. then it should not break like
Dr.
Harry is going.
It should be Dr.Harry is going. as it is.
just like I have some words, if they come in string then it should not break
StringBuffer regex = new StringBuffer("Dr[\\.]|Gr[\\.]|[Aa][\\.][Mm][\\.]|"+ "[Pp][\\.][Mm][\\.]|Emp[\\.]|Rs[\\.]|Ms[\\.]|No[\\.]|Nos[\\.]|"+ "Dt[\\.]|Sh[\\.]|(Mr|mr)[\\.]|(Mrs|mrs)[\\.]|Admn[\\.]|Ad[\\.]|Smt[\\.]|"+ "GOVT[\\.]|Govt[\\.]|Deptt[\\.]|Tel[\\.]|Secy[\\.]|Estt[\\.]|"+ "Asstt[\\.]|Hqrs[\\.]|DY[\\.]|Supdt[\\.]|w[\\.]e[\\.]f[\\.]|"+ "I[\\.]|N[\\.]|[0-9]+[\\.][0-9]+[\\.][0-9]|K[\\.]|NSI[\\.]|"+ "Prof[\\.]|Dte[\\.]|no[\\.]|nos[\\.]|Agri[\\.]|R[\\.]|"+ "K[\\.]|Y[\\.]|C[\\.]|N[\\.]|Dept[\\.]|S[\\.]|Spl[\\.]|N[\\.]|"+ "Sr[\\.]|Addl[\\.]|i[\\.]e[\\.]|Sl[\\.]|CS[\\.]|M[\\.]|IPS[\\.]|"+ "Jt[\\.]|viz[\\.]|hrs[\\.]|S/Sh[\\.]|Jr[\\.]|E[\\.]|S[\\.]|"+ "Pers[\\.]|Deptts[\\.]|OM[\\.]|DT[\\.]|Proj[\\.]|Instrum[\\.]|"+ "Div[\\.]|Dev[\\.]|Env[\\.]|e[\\.]g[\\.]|etc[\\.]|Misc[\\.]|"+ "vig[\\.]|Dr[\\.]|Nos[\\.]|Ltd[\\.]|Maj[\\.]|"+ "Gen[\\.]|MAJ[\\.]|GEN[\\.]|Su[\\.]|/Ess[\\.]|Com[\\.]|St[\\.]|");
these are some words in which string should not split if they come. just like Dr.Harry is going.
Any regular expression is possible ?
or any other method ?
thanks
use this :
search : (?<!(Mr|Dr|Gr|Aa))\.
replace : \n
you can add as many words you want using | after the Aa.
demo here : http://regex101.com/r/fP6hN9
I tried the code below and its working fine for me:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String str1 = "We are going there.How are you.Mr.Gordon is also coming with us.Are you sure you want to take him", str2;
String substr = "\n", regex = "(?<!(Mr|Dr|Gr|Aa))\\.";
// prints string1
System.out.println("String = " + str1);
/* replaces each substring of this string that matches the given
regular expression with the given replacement */
str2 = str1.replaceAll(regex, substr);
System.out.println("After Replacing = " + str2);
}
}
outputs :
We are going there
How are you
Mr.Gordon is also coming with us
Are you sure you want to take him
checked here : http://ideone.com/YfLU7v
Use this regex:
(?<!(Dr|Gr|Aa|Mm|Pp))\.
Fill in the rest as required. This uses Lookaround
in java, I am trying to find if a given string has one of the many sub strings using multiple ORs in a single If condition and if any of the sub string exists, remove it. I am not sure how to do it. Also, this string search needs to be case insensitive.
Here is the sample code
if (inputString contains any of the subStrings i.e. "_LOCATION" OR "_MANAGEMENT" Or "_ZIPCODE")
{
remove the subString from inPutString
}
Ex: Given the string - "STATE_CAPITAL_LOCATION_MANAGEMENT_PHONE_EMAIL_zipcode"
Resulting string should be - "STATE_CAPITAL_PHONE_EMAIL"
What is the best way to do it.
Thanks
Using separate If statements makes more easier.
Try this code:
String a="STATE_CAPITAL_LOCATION_MANAGEMENT_PHONE_EMAIL_zipcode";
if(a.contains("_LOCATION"))// relace with your string
{
a=a.replace("_LOCATION","");
System.out.println(a);
}
if(a.contains("_MANAGEMENT"))// relace with your string
{
a=a.replace("_MANAGEMENT","");
System.out.println(a);
}
// .....
I'm using boost::regex to parse some formatting string where '%' symbol is escape character. Because I do not have much experience with boost::regex, and with regex at all to be honest I do some trial and error. This code is some kind of prototype that I came up with.
std::string regex_string =
"(?:%d\\{(.*)\\})|" //this group will catch string for formatting time
"(?:%([hHmMsSqQtTlLcCxXmMnNpP]))|" //symbols that have some meaning
"(?:\\{(.*?)\\})|" //some other groups
"(?:%(.*?)\\s)|"
"(?:([^%]*))";
boost::regex regex;
boost::smatch match;
try
{
regex.assign(regex_string, boost::regex_constants::icase);
boost::sregex_iterator res(pattern.begin(), pattern.end(), regex);
//pattern in line above is string which I'm parsing
boost::sregex_iterator end;
for(; res != end; ++res)
{
match = *res;
output << match.get_last_closed_paren();
//I want to know if the thing that was just written to output is from group describing time string
output << "\n";
}
}
catch(boost::regex_error &e)
{
output<<"regex error\n";
}
And this works pretty good, on the output I have exactly what I want to catch. But I do not know from which group it is. I could do something like match[index_of_time_group]!="" but this is kind of fragile, and doesn't look too good. If I change regex_string index that was pointing on group catching string for formatting time could also change.
Is there a neat way to do this? Something like naming groups? I'll be grateful for any help.
You can use boost::sub_match::matched bool member:
if(match[index_of_time_group].matched) process_it(match);
It is also possible to use named groups in regexp like: (?<name_of_group>.*), and with this above line could be changed to:
if(match["name_of_group"].matched) process_it(match);
Dynamically build regex_string from pairs of name/pattern, and return a name->index mapping as well as the regex. Then write some code that determines if the match comes from a given name.
If you are insane, you can do it at compile time (the mapping from tag to index that is). It isn't worth it.
I'm working with a multi line text block where I need to divide everything into 3 groups
1: beginning of the file up to a string literal // don't keep
2: The next line //KEEP THE LINE FOLLOWING STRING LITERAL
3: Everything following that line to the end of file. // don't keep
<<
aFirstLing here
aSecondLine here
MyStringLiteral //marks the next line as the target to keep
What I want to Keep!
all kinds of crap that I don't
<<
I'm finding plenty of ways to pull from the beginning of a line but am unable to see how to include an unknown number of non-blank lines until I reach that string literal.
EDIT: I'm removing the .net-ness to focus on regex only. Perhaps this is a place for understanding backreferences?
Rather than read the entire file into memory, just read what you need:
List<string> TopLines = new List<string>();
string prevLine = string.Empty;
foreach (var link in File.ReadLines(filename))
{
TopLines.Add(line);
if (prevLine == Literal)
{
break;
}
prevLine = line;
}
I suppose there's a LINQ solution, although I don't know what it is.
EDIT:
If you already have the text of the email in you application (as a string), you have to split it into lines first. You can do that with String.Split, splitting on newlines, or you can create a StringReader and read it line-by-line. The logic above still applies, but rather than File.ReadLines, just use foreach on the array of lines.
EDIT 2:
The following LINQ might do it:
TopLines = File.ReadLines(filename).TakeWhile(s => s != Literal).ToList();
TopLines.Add(Literal);
Or, if the strings are already in a list:
TopLines = lines.TakeWhile(s => s != Literal).ToList();
TopLines.Add(Literal);
.*(^MyStringLiteral\r?\n)([\w|\s][^\r\n]+)(.+) seems to work. the trick wasn't back references - it was the exclusion of \r\n.
File.ReadAllLines() will give you an array you can iterate over until you find your literal, then take the next line
string[] lines = File.ReadAllLines();
for(int i;i<lines.Length;i++)
{
if(line == Literal)
return lines[i + 1];
}
What's the easiest way to do an "instring" type function with a regex? For example, how could I reject a whole string because of the presence of a single character such as :? For example:
this - okay
there:is - not okay because of :
More practically, how can I match the following string:
//foo/bar/baz[1]/ns:foo2/#attr/text()
For any node test on the xpath that doesn't include a namespace?
(/)?(/)([^:/]+)
Will match the node tests but includes the namespace prefix which makes it faulty.
I'm still not sure whether you just wanted to detect if the Xpath contains a namespace, or whether you want to remove the references to the namespace. So here's some sample code (in C#) that does both.
class Program
{
static void Main(string[] args)
{
string withNamespace = #"//foo/ns2:bar/baz[1]/ns:foo2/#attr/text()";
string withoutNamespace = #"//foo/bar/baz[1]/foo2/#attr/text()";
ShowStuff(withNamespace);
ShowStuff(withoutNamespace);
}
static void ShowStuff(string input)
{
Console.WriteLine("'{0}' does {1}contain namespaces", input, ContainsNamespace(input) ? "" : "not ");
Console.WriteLine("'{0}' without namespaces is '{1}'", input, StripNamespaces(input));
}
static bool ContainsNamespace(string input)
{
// a namspace must start with a character, but can have characters and numbers
// from that point on.
return Regex.IsMatch(input, #"/?\w[\w\d]+:\w[\w\d]+/?");
}
static string StripNamespaces(string input)
{
return Regex.Replace(input, #"(/?)\w[\w\d]+:(\w[\w\d]+)(/?)", "$1$2$3");
}
}
Hope that helps! Good luck.
Match on :? I think the question isn't clear enough, because the answer is so obvious:
if(Regex.Match(":", input)) // reject
You might want \w which is a "word" character. From javadocs, it is defined as [a-zA-Z_0-9], so if you don't want underscores either, that may not work....
I dont know regex syntax very well but could you not do:
[any alpha numeric]\*:[any alphanumeric]\*
I think something like that should work no?
Yeah, my question was not very clear. Here's a solution but rather than a single pass with a regex, I use a split and perform iteration. It works as well but isn't as elegant:
string xpath = "//foo/bar/baz[1]/ns:foo2/#attr/text()";
string[] nodetests = xpath.Split( new char[] { '/' } );
for (int i = 0; i < nodetests.Length; i++)
{
if (nodetests[i].Length > 0 && Regex.IsMatch( nodetests[i], #"^(\w|\[|\])+$" ))
{
// does not have a ":", we can manipulate it.
}
}
xpath = String.Join( "/", nodetests );