Sponsored Content
Top Forums Programming RegEx in Java. \b does not react to a newline Post 302421243 by durden_tyler on Friday 14th of May 2010 12:35:40 AM
Old 05-14-2010
Works fine for me:

Code:
$ 
$ 
$ # show the Java program
$ cat -n RegexTest.java
     1    import java.util.regex.Matcher;
     2    import java.util.regex.Pattern;
     3    public class RegexTest {
     4      public static void main(String[] args) {
     5        String str = "hello and bye\nand\nblah";
     6        String ptn = "\\b\\w+\\b";
     7        int num = 0;
     8        Pattern pattern = Pattern.compile(ptn);
     9        Matcher matcher = pattern.matcher(str);
    10        System.out.println("=======  String for inspection  =======");
    11        System.out.println(str);
    12        System.out.println("========================================");
    13        System.out.println("=======  Pattern to be searched  =======");
    14        System.out.println(ptn);
    15        System.out.println("========================================");
    16        boolean found = false;
    17        while (matcher.find()) {
    18          num++;
    19          System.out.println("Match # "+num+"\t:\t"+matcher.group()+"\t=>\tstarts at "
    20              + "index "+matcher.start()+" and ends at index "+matcher.end());
    21          found = true;
    22        }
    23        if (!found) {
    24          System.out.println("No match found");
    25        }
    26      }
    27    }
$ 
$ # compile
$ javac RegexTest.java
$ 
$ # run
$ java RegexTest
=======  String for inspection  =======
hello and bye
and
blah
========================================
=======  Pattern to be searched  =======
\b\w+\b
========================================
Match # 1    :    hello    =>    starts at index 0 and ends at index 5
Match # 2    :    and    =>    starts at index 6 and ends at index 9
Match # 3    :    bye    =>    starts at index 10 and ends at index 13
Match # 4    :    and    =>    starts at index 14 and ends at index 17
Match # 5    :    blah    =>    starts at index 18 and ends at index 22
$ 
$

tyler_durden
 

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

2. Shell Programming and Scripting

Modify or react on "cd" command

Hi folks, I found a pretty nice thread in this forum showing how to change the window title of a PuTTY window from within the shell. (Which I can't reference due having less than 5 postings?!?) I modified the solution a bit to have a function in my zshrc "wt()" changing the window title each... (8 Replies)
Discussion started by: mephistho
8 Replies

3. Shell Programming and Scripting

Add newline to regex

I have a perl script that runs a program and puts the output of the program into a variable. The output of the file looks like: Statistics 0 AverageTime: Statistics 0 AverageTime: I'm trying to run a regular express against this to pull out the first value in each of the parens... (1 Reply)
Discussion started by: Boomn4x4
1 Replies

4. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

5. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

6. Web Development

Google Trends: react.js angular.js vue.js

While I'm on the subject of Google trends, here is a global trend since 2004 comparing react.js, angular.js, vue.js It's no secret I'm a vue.js fan and coder, but not because of the trend line (which I just saw for the first time a few minutes ago) My experience is that vue.js, a late arrival... (0 Replies)
Discussion started by: Neo
0 Replies

7. UNIX for Beginners Questions & Answers

Pass RegEx to java program in bash script

I can't seem to get this right. I've tried it every way imaginable using every trick I see on stackexchange and such. No luck. So nothing major here, something like: #!/bin/bash SEARCH="ARG1 ARG2 '((^EXACT$)|(.*InTheMiddle*)|(^AtBeginning*))'" java -cp /my/class/path MyClassName $SEARCH... (3 Replies)
Discussion started by: stonkers
3 Replies
DACS.JAVA(7)						  DACS Miscellaneous Information					      DACS.JAVA(7)

NAME
dacs.java - DACS Java support DESCRIPTION
This document describes support for Java provided by DACS. To enable Java support, DACS must be configured using the --enable-java flag. It may also be necessary to specify the location of the Java commands and include files. See dacs.install(7)[1]. Note Java support is currently very limited and should be considered experimental. It is currently only possible to call dacsauth(1)[2] and dacscheck(1)[3] using the Java Native Interface (JNI) provided. The command line arguments to both of these commands are exactly as documented in their manual pages. Each argument is passed as a String. The code is not thread-safe and is probably not suitable for being called many times within a particular process due to assumptions it makes about dynamic memory allocation. There are no immediate plans to improve or extend Java support. Using the existing capability to build the JNI layer, however, it should not be difficult for an experienced Java programmer to build upon the example Java code provided. With Java support enabled, building DACS will copy the JNI shared library and a .jar file to the library directory (default: /usr/local/dacs/lib), and the JNI include files to the include directory (default: /usr/local/dacs/include). The tools/java/jni distribution directory contains a simple script called javarun that demonstrates how to run dacsauth or dacscheck as a Java application. For example, to validate the Unix password for user somebody, run (as root): ./javarun dacsauth -m unix suff -user somebody -prompt DIAGNOSTICS
The return codes are as documented in the command's manual page. AUTHOR
Distributed Systems Software (www.dss.ca[4]) COPYING
Copyright2003-2012 Distributed Systems Software. See the LICENSE[5] file that accompanies the distribution for licensing information. NOTES
1. dacs.install(7) http://dacs.dss.ca/man/dacs.install.7.html 2. dacsauth(1) http://dacs.dss.ca/man/dacsauth.1.html 3. dacscheck(1) http://dacs.dss.ca/man/dacscheck.1.html 4. www.dss.ca http://www.dss.ca 5. LICENSE http://dacs.dss.ca/man/../misc/LICENSE DACS 1.4.27b 10/22/2012 DACS.JAVA(7)
All times are GMT -4. The time now is 06:33 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy