Git Product home page Git Product logo

jpr_01's Introduction

jpr_01

The library can be used to retrieve the offsets of the input string that are matched against the given regular expression, and it can also retrieve multiple named groups within one matching operation. This information can be used for extracting information from the input string, or replacing parts of the input string.

Building

Tested on Fedora 36

dnf install pcre2-devel
mvn clean package

Adding the library to your project

To use the library, it can be added via Maven:

<dependency>
      <groupId>com.teragrep</groupId>
      <artifactId>jpr_01</artifactId>
      <version>3.0.1</version>
</dependency>

After Maven has downloaded and setup the package, it can be imported and the JavaPcre object instantialized:

import com.teragrep.jpr_01.JavaPcre;

JavaPcre pcre = new JavaPcre();

Pattern compilation

The regex matcher can be compiled for a given pattern using standard PCRE2 syntax

final String pattern = "^Hello.*$";
pcre.compile_java(pattern);

To clear the compiled pattern, the jcompile_free() function can be used

pcre.jcompile_free();

Matching

To attempt matching the pattern, use the singlematch_java() function. The second argument specifies the offset, from which point of the String to start matching. To start from the beginning, use 0 (zero).

final String input = "Hello this is a input";
pcre.singlematch_java(input, 0);

Extraction

If named capture groups are used, the results can be found via the match table and name table. The match table contains all the matches with unique IDs. Name table connects such unique IDs with any capture groups if given.

Map<String, Integer> nameTable = pcre.get_name_table(); // e.g. NameOfTeacher=1, NameOfAssistant=2
Map<Integer, String> matchTable = pcre.get_match_table(); // e.g. 1="Anna", 2="Robert"

For example, these maps can be used to get the groups and their contents mapped into one:

final Map<String, String> results = new HashMap<>();
for (final Map.Entry<String, Integer> me : nameTable.entrySet()) {
    final String value = matchTable.get(me.getValue());
    final String name = me.getKey();
    results.put(name, value); // e.g. columnName=value
}

Match checking and location of matches

To check whether or not there is a match, the get_matchfound() function can be used.

boolean hasMatch = pcre.get_matchfound();

For multiple matches, the get_matchfound() function can be used in conjunction with the singlematch_java()'s offset argument.

int offset = 0;
pcre.singlematch_java(inputStr, offset);
while (pcre.get_matchfound()) {
    offset = pcre.get_ovector1();
    pcre.singlematch_java(inputStr, offset);
}

Offsets

The offsets (match starting index, match ending index) can be grabbed with the get_ovector0() and get_ovector1() functions.

int start = pcre.get_ovector0();
int end = pcre.get_ovector1();

jpr_01's People

Contributors

eemhu avatar kortemik avatar ronja-ui avatar tiihott avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kortemik eemhu

jpr_01's Issues

cannot find native library LibJavaPcre.so

on jpr_01 3.0.0, JavaPcre pcre = new JavaPcre(); causes

java.lang.UnsatisfiedLinkError: Unable to load library 'JavaPcre':
libJavaPcre.so: cannot open shared object file: No such file or directory
libJavaPcre.so: cannot open shared object file: No such file or directory
Native library (linux-x86-64/libJavaPcre.so) not found in resource path (...)
	at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:307)
	at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:467)
	at com.sun.jna.Library$Handler.<init>(Library.java:192)
	at com.sun.jna.Native.load(Native.java:622)
	at com.sun.jna.Native.load(Native.java:596)
	at com.teragrep.jpr_01.JavaPcre$LibJavaPcre.<clinit>(JavaPcre.java:39)
	at sun.misc.Unsafe.ensureClassInitialized(Native Method)
	at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
	at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:156)
	at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1088)
	at java.lang.reflect.Field.getFieldAccessor(Field.java:1069)
	at java.lang.reflect.Field.get(Field.java:393)
	at com.sun.jna.Native.loadLibraryInstance(Native.java:694)
	at com.sun.jna.Native.getLibraryOptions(Native.java:764)
	at com.sun.jna.Native.getStructureAlignment(Native.java:856)
	at com.sun.jna.Structure.setAlignType(Structure.java:291)
	at com.sun.jna.Structure.<init>(Structure.java:208)
	at com.sun.jna.Structure.<init>(Structure.java:204)
	at com.sun.jna.Structure.<init>(Structure.java:191)
	at com.sun.jna.Structure.<init>(Structure.java:183)
	at com.teragrep.jpr_01.JavaPcre$LibJavaPcre$OptionsStruct.<init>(JavaPcre.java:42)
	at com.teragrep.jpr_01.JavaPcre.<init>(JavaPcre.java:176)

change malloc/free pairs to use AutoCloseable

change the Java classes to use AutoCloseable interface so that pointers get automatically freed

see https://www.yegor256.com/2017/08/08/raii-in-java.html

then usage of the resources should happen with try-with-resources which ensures their closing

try (final SomeClass sc = new SomeClass("perhaps arg").open()) { // allocates memory, open returns allocated instance of the class
System.out.println("you have call()ed and it provides you with " + sc.something());
} // frees it because close is automatically done

care must be taken that SomeClass methods are blocked while allocation is not done, for example with AtomicBoolean and if not the thr IllegalStateException

see also https://stackoverflow.com/a/69444249

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.