d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Regex Help > Eh..
12Next
Add Reply New Topic New Poll
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Sep 2 2015 03:55pm
So I'm trying to create a key map using this as like sample data:
Code
<<COMPANY_NAME>>:Great Lakes,<<USER_NAME>>:superCoder
<<ID>>:738390,<<CONTACT_NAME>>:Frank Phillips


These two regex expressions I got are close...

Code

\<([^<>]+) breaks up like: <COMPANY_NAME
\>([^<>]+) breaks up like: >:Great Lakes,


BUT I would like my expressions to yield
COMPANY_NAME
Great Lakes,

Not sure what to change been trying that RegExr site without any luck for quite some time. I'm pretty sure what I'm trying to do is possible -- anyone know what my expressions should look like?

Maybe there's a better approach I'm basically using a String.split(regex) to create what key maps id like...

any type of speculation is appreciated!
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 2 2015 06:48pm
using rubular, I got


Code
([^<>:,]+)


which yields


Code
Match 1
1. COMPANY_NAME
Match 2
1. Great Lakes
Match 3
1. USER_NAME
Match 4
1. superCoder
Match 5
1. ID
Match 6
1. 738390
Match 7
1. CONTACT_NAME
Match 8
1. Frank Phillips


an optimistic regex which basically assumes none of those fields can have a legit colon, arrow, or comma as part of the string

This post was edited by Eep on Sep 2 2015 06:49pm
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Sep 8 2015 09:40am
Quote (Eep @ Sep 2 2015 07:48pm)
using rubular, I got


Code
([^<>:,]+)


an optimistic regex which basically assumes none of those fields can have a legit colon, arrow, or comma as part of the string


Thanks for the reply. I think this will work great for what I'm trying to do in my assignment.

After I submit my assignment I'll post what it was / what I did and hope for feedback on what I could of did / general coding suggestions.

Thanks again!
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Sep 9 2015 04:29pm
Alrigt so I'mma post my assignment that I just completed. Any suggestions on different approaches I could of taken?

Oh and don't mind the terrible coding standards! I didn't break stuff out of main + nested stuff...


-findandreplace.txt
Code
<<COMPANY_NAME>>:Great Lakes,<<USER_NAME>>:superCoder
<<ID>>:738390,<<CONTACT_NAME>>:Frank Phillips


-input.txt
Code
This is a file to use to test out the find and replace application

The next two lines should have values swapped in:
<<COMPANY_NAME>><<USER_NAME>><<ID>><<CONTACT_NAME>>
<<COMPANY_NAME>><<USER_NAME>><<ID>><<CONTACT_NAME>>

No find and replace should happen on the next line
COMPANY_NAME USER_NAME ID CONTACT_NAME

More testing with the same field listed multiple times on a single line <<USER_NAME>> <<USER_NAME>>

Add more of your own "tests" here...



Code
package com.company;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

public static void main(String[] args) throws Exception {
Scanner originalTxtFile = new Scanner(new File(args[0]));
Scanner lookupTxtFile = new Scanner(new File(args[1]));

FileWriter replacementFile = new FileWriter(createReplacedFile());

Map<String,String>lookupKeyValuesMap = createLookupKeyValues(lookupTxtFile);// passes scanner object.

StringBuilder builder = new StringBuilder();

while (originalTxtFile.hasNext()) {
String currentLine = originalTxtFile.nextLine();
String[] currentWordsInLine = currentLine.split("([<>:,]+)");

for(String word : currentWordsInLine) {
if(lookupKeyValuesMap.containsKey(word)) {
word = lookupKeyValuesMap.get(word)+ " ";
}
builder.append(word);
}

builder.append("\n");
}

replacementFile.write(builder.toString());
replacementFile.close();
}

private static File createReplacedFile() throws Exception {
File replacedFile = new File("replaced.txt");
replacedFile.createNewFile();
return replacedFile;
}

private static Map<String,String> createLookupKeyValues(Scanner lookupScanner) throws Exception {
Map<String,String> lookupKeyValuesMap = new HashMap<String, String>();

ArrayList<String> keys = new ArrayList<String>();
ArrayList<String> values = new ArrayList<String>();

while(lookupScanner.hasNext()) {
String[] currentWordsInLine = lookupScanner.nextLine().split("([<>:,]+)");

for (int i=0; i<currentWordsInLine.length; i++) {
if (i%2 == 0) {
if (currentWordsInLine[i].length() != 0) {
values.add(currentWordsInLine[i]);
}
} else {
keys.add(currentWordsInLine[i]);
}
}
}

for (int i=0; i<keys.size(); i++) {
lookupKeyValuesMap.put(keys.get(i),values.get(i));
}

return lookupKeyValuesMap;
}
}


e* what i have works / meets specs - but I feel there had to be something more straightforward.

This post was edited by Noobtard on Sep 9 2015 04:30pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 10 2015 07:03pm
Different approach? Straightforward?

Code
public class ContentReplacerDriver
{
public static void main (String[] args) throws java.lang.Exception
{
if(args.length != 2)
throw new IllegalArgumentException("args must contain replacements file and content file");

String replacementFileName = args[0];
String contentFileName = args[1];

ContentReplacer cr = new ContentReplacer(replacementFileName);

String replaced = cr.replace(contentFileName);

System.out.println(replaced);
}
}

Code
import java.util.*;
import java.io.*;

public class ContentReplacer
{
private ContentReplaceFile contentReplaceFile;

public ContentReplacer(String fileName) throws FileNotFoundException, IOException
{
this(fileName, new DefaultContentReplaceFileFactory());
}

public ContentReplacer(String fileName, ContentReplaceFileFactory contentReplaceFileFactory)
throws FileNotFoundException, IOException
{
if(fileName == null || fileName.isEmpty())
throw new IllegalArgumentException("fileName is null or empty");
if(contentReplaceFileFactory == null)
throw new IllegalArgumentException("contentReplaceFileFactory is null");

this.contentReplaceFile = contentReplaceFileFactory.createContentReplaceFile(fileName);
}

public String replace(String fileName) throws FileNotFoundException
{
Map<String, String> replacements = contentReplaceFile.getReplacements();

Scanner in = new Scanner(new File(fileName));

String contentToReplace = new String();

while(in.hasNextLine())
{
contentToReplace += in.nextLine() + "\n";
}

for(Map.Entry<String, String> kvp : replacements.entrySet())
{
String key = kvp.getKey();
String value = kvp.getValue();

contentToReplace = contentToReplace.replace(key, value);
}

return contentToReplace;
}
}

Code
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import java.util.Scanner;



public class ContentReplaceFile
{
private Map<String, String> replacements;
private String kvDelimiter, kvpDelimiter;

private static final String DEFAULT_KVP_DELIMITER = ",";
private static final String DEFAULT_KV_DELIMITER = ":";

public ContentReplaceFile(String fileName)
throws FileNotFoundException, IOException
{
this(fileName, DEFAULT_KVP_DELIMITER, DEFAULT_KV_DELIMITER);
}

public ContentReplaceFile(String fileName, String kvpDelimiter, String kvDelimiter)
throws FileNotFoundException, IOException
{
if(kvpDelimiter == null || kvpDelimiter.isEmpty())
throw new IllegalArgumentException("kvpDelimiter is null or empty");
if(kvDelimiter == null || kvDelimiter.isEmpty())
throw new IllegalArgumentException("kvDelimiter is null or empty");

this.kvpDelimiter = kvpDelimiter;
this.kvDelimiter = kvDelimiter;

loadFile(fileName);
}

private void loadFile(String fileName) throws FileNotFoundException, IOException
{
Scanner in = new Scanner(new File(fileName));

replacements = new Hashtable<String, String>();

in.useDelimiter(kvpDelimiter);

while(in.hasNext())
{
String kvp = in.next();
String[] kv = kvp.split(kvDelimiter);

if(kv.length != 2)
throw new IOException(String.format("format should be key%svalue", kvDelimiter));

replacements.put(kv[0], kv[1]);
}
}

public Map<String, String> getReplacements()
{
return new Hashtable<String, String>(replacements);
}
}

Code
import java.io.FileNotFoundException;
import java.io.IOException;


public interface ContentReplaceFileFactory
{
ContentReplaceFile createContentReplaceFile(String fileName) throws FileNotFoundException, IOException;
}

Code
import java.io.FileNotFoundException;
import java.io.IOException;

public class DefaultContentReplaceFileFactory implements ContentReplaceFileFactory
{
public ContentReplaceFile createContentReplaceFile(String fileName) throws FileNotFoundException, IOException
{
return new ContentReplaceFile(fileName);
}
}


Hi guys.

This post was edited by Minkomonster on Sep 10 2015 07:05pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 10 2015 07:33pm
you didn't call, you didn't write, you didn't troll anyone. i thought you retired to a fishing boat off costa rica.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 10 2015 07:53pm
Friends convinced me to play new WoW expansion. That lasted a couple months, and then I just kind of fell off for a while.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 10 2015 08:25pm
Quote (Minkomonster @ Sep 10 2015 09:53pm)
Friends convinced me to play new WoW expansion. That lasted a couple months, and then I just kind of fell off for a while.


hopefully not depression?

i went over to my neighbor's to play with her bulldog. it's so lazy and fits my personality so well. i'm thinking about getting a basset hound. also lazy, but has cute floppy ears.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 10 2015 08:29pm
ROBUST BUSINESS SOLUTIONS BACK IN TOWN
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Sep 10 2015 08:53pm
Quote (carteblanche @ Sep 10 2015 09:25pm)
hopefully not depression?

i went over to my neighbor's to play with her bulldog. it's so lazy and fits my personality so well. i'm thinking about getting a basset hound. also lazy, but has cute floppy ears.


Nah, not depression. Just work and shit. Haven't been doing much of anything else.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll