Skip to content

Instantly share code, notes, and snippets.

@Groostav
Created August 7, 2014 04:45
Show Gist options
  • Save Groostav/acf5b584078813e7cbe6 to your computer and use it in GitHub Desktop.
Save Groostav/acf5b584078813e7cbe6 to your computer and use it in GitHub Desktop.
Request for help with regexes
package com.EmpowerOperations.LanguageAndLibraryTests;
import com.EmpowerOperations.Common.Ref;
import com.EmpowerOperations.LinqALike.Common.Formatting;
import com.EmpowerOperations.LinqALike.Queryable;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.EmpowerOperations.LinqALike.Factories.from;
import static com.EmpowerOperations.LinqALike.Factories.range;
/**
* Created by Geoff on 2014-08-06.
*/
public class RegexFixture {
private Queryable<String> simpleNames = from("x1", "longVarName2");
private String sourceText = "" +
//this was taken from our sample problem, Optim.m, which is an APDL file
"!!!! This is the kerl of optimization for sheet metal parts assembly\n" +
"!!!!\n" +
"\n" +
"!!!!!!!! locator's position P1(p1x,p1y),P2(p2x,p2y)\n" +
"!!!!!!!! Welding spot= s\n" +
"\n" +
"p1x=x1/1000.0\n" +
"p1y=x2/1000.0\n" +
"\n" +
"p2x=x3/1000.0\n" +
"p2y=x4/1000.0\n" +
"\n" +
"s=x5/1000.0\n" +
"\n" +
"\n" +
"\n" +
"/PREP7 \n" +
"!* \n" +
"ET,1,SHELL63\n" +
// manually added this line{
"ET,MPTEMP,,,,EX, x1=38000\n" +
// } end manual edit
"!* \n" +
"!* \n" +
"R,1,0.001,0.001,0.001,0.001, , ,\n" +
"RMORE, , , ,\n" +
"RMORE \n" +
"RMORE, ,\n" +
"!* \n" +
"!* \n" +
"MPTEMP,,,,,,,, \n" +
"MPTEMP,1,0 \n" +
"MPDATA,EX,1,,38000*6894.75\n" +
"MPDATA,PRXY,1,,0.3 \n" +
"RECTNG,0,0.1,0,0.1,";
private final String regex = " +(?<variableName>\\w+) *= *-?(?<variableValue>\\d+\\.?\\d*)";
@Test
public void when_scanning_using_our_regex(){
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(sourceText);
StringBuffer output = new StringBuffer();
int newValue = 20;
while(matcher.find()){
String found = matcher.group();
int start = matcher.start();
int end = matcher.end();
int groupCount = matcher.groupCount(); //this does NOT return the number of groups, it returns the index of the last group!!!
Queryable<String> groups = from(range(0, matcher.groupCount() + 1)).select(matcher::group);
Ref<Integer> displayedIndex = new Ref<>(0);
String groupsListed = Formatting.verticallyPrintMembers(groups.select(group -> "" + displayedIndex.target++ + ": " + group));
System.out.println(
"found:'" + found + "' at " +
"start:" + start + " , " +
"end:" + end + " " +
"groups:" + groupCount + ", \n" +
"\t" + groupsListed + "\n" +
"near '" + sourceText.substring(Math.max(0, start - 10), Math.min(end + 10, sourceText.length() - 1)) + "'");
String existingValue = matcher.group("variableValue");
matcher.appendReplacement(output, "" + newValue);
}
matcher.appendTail(output);
System.out.println(output.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment