Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #28787 > unrolled thread
| Started by | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| First post | 2014-06-19 21:39 -0700 |
| Last post | 2014-08-01 02:45 -0700 |
| Articles | 11 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
Regex? Knute Johnson <eternal@knutejohnson.com> - 2014-06-19 21:39 -0700
Re: Regex? Knute Johnson <eternal@knutejohnson.com> - 2014-06-19 22:03 -0700
Re: Regex? Robert Klemme <shortcutter@googlemail.com> - 2014-06-20 09:25 +0200
Re: Regex? Knute Johnson <eternal@knutejohnson.com> - 2014-06-20 08:06 -0700
Re: Regex? Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-06-20 11:14 -0400
Re: Regex? Knute Johnson <eternal@knutejohnson.com> - 2014-06-20 15:46 -0700
Re: Regex? Roedy Green <see_website@mindprod.com.invalid> - 2014-06-25 08:15 -0700
Re: Regex? Knute Johnson <eternal@knutejohnson.com> - 2014-06-25 18:10 -0700
Re: Regex? Knute Johnson <eternal@knutejohnson.com> - 2014-06-30 09:52 -0700
Re: Regex? Roedy Green <see_website@mindprod.com.invalid> - 2014-06-21 09:49 -0700
Re: Regex? Roedy Green <see_website@mindprod.com.invalid> - 2014-08-01 02:45 -0700
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-06-19 21:39 -0700 |
| Subject | Regex? |
| Message-ID | <lo0du6$os5$1@dont-email.me> |
How do you search for a String that contains a character such as ( that is special to a regex? Thanks, -- Knute Johnson
[toc] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-06-19 22:03 -0700 |
| Message-ID | <lo0fac$vgk$1@dont-email.me> |
| In reply to | #28787 |
Pattern.compile(Pattern.quote("the string with the ) in it"));
I hate that, look for it for an hour, write something here and then come
across the answer 2 minutes later.
k...
On 6/19/2014 21:39, Knute Johnson wrote:
> How do you search for a String that contains a character such as ( that
> is special to a regex?
>
> Thanks,
>
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2014-06-20 09:25 +0200 |
| Message-ID | <c0i5vfFqriiU1@mid.individual.net> |
| In reply to | #28788 |
On 06/20/2014 07:03 AM, Knute Johnson wrote:
> Pattern.compile(Pattern.quote("the string with the ) in it"));
There are more ways:
Pattern.compile("the string with the \\) in it");
Pattern.compile("the string with the \\Q)\\E in it");
Cheers
robert
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-06-20 08:06 -0700 |
| Message-ID | <lo1im5$3ak$1@dont-email.me> |
| In reply to | #28789 |
On 6/20/2014 00:25, Robert Klemme wrote:
> On 06/20/2014 07:03 AM, Knute Johnson wrote:
>> Pattern.compile(Pattern.quote("the string with the ) in it"));
>
> There are more ways:
>
> Pattern.compile("the string with the \\) in it");
> Pattern.compile("the string with the \\Q)\\E in it");
>
> Cheers
>
> robert
Thanks Robert,
The problem was actually
String str = "(something";
Pattern.compile(str);
so now it is
Pattern.compile(Pattern.quote(str));
I hate to make them
Pattern.compile(str+"\\Q")
(the cognoscenti would have my ...)
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2014-06-20 11:14 -0400 |
| Message-ID | <lo1j4g$6l6$1@dont-email.me> |
| In reply to | #28793 |
On 6/20/2014 11:06 AM, Knute Johnson wrote:
> On 6/20/2014 00:25, Robert Klemme wrote:
>> On 06/20/2014 07:03 AM, Knute Johnson wrote:
>>> Pattern.compile(Pattern.quote("the string with the ) in it"));
>>
>> There are more ways:
>>
>> Pattern.compile("the string with the \\) in it");
>> Pattern.compile("the string with the \\Q)\\E in it");
>>
>> Cheers
>>
>> robert
>
> Thanks Robert,
>
> The problem was actually
>
> String str = "(something";
> Pattern.compile(str);
>
> so now it is
>
> Pattern.compile(Pattern.quote(str));
>
> I hate to make them
>
> Pattern.compile(str+"\\Q")
>
> (the cognoscenti would have my ...)
Something seems peculiar: If the cognoscenti who provide the
search strings want all characters to stand for themselves and
not to function as regex metacharacters, what's the point of
using a regex? Why not use theString.indexOf(str)? Is there
more to the use case than we've been told?
--
Eric Sosman
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-06-20 15:46 -0700 |
| Message-ID | <lo2dji$k7u$1@dont-email.me> |
| In reply to | #28796 |
On 6/20/2014 08:14, Eric Sosman wrote:
> On 6/20/2014 11:06 AM, Knute Johnson wrote:
>> On 6/20/2014 00:25, Robert Klemme wrote:
>>> On 06/20/2014 07:03 AM, Knute Johnson wrote:
>>>> Pattern.compile(Pattern.quote("the string with the ) in it"));
>>>
>>> There are more ways:
>>>
>>> Pattern.compile("the string with the \\) in it");
>>> Pattern.compile("the string with the \\Q)\\E in it");
>>>
>>> Cheers
>>>
>>> robert
>>
>> Thanks Robert,
>>
>> The problem was actually
>>
>> String str = "(something";
>> Pattern.compile(str);
>>
>> so now it is
>>
>> Pattern.compile(Pattern.quote(str));
>>
>> I hate to make them
>>
>> Pattern.compile(str+"\\Q")
>>
>> (the cognoscenti would have my ...)
>
> Something seems peculiar: If the cognoscenti who provide the
> search strings want all characters to stand for themselves and
> not to function as regex metacharacters, what's the point of
> using a regex? Why not use theString.indexOf(str)? Is there
> more to the use case than we've been told?
>
Well I've been round and round on this, I thought originally that I
could find all the examples of the search string with one call to
Matcher.matches() but I can't figure out how to do that. It may be
possible but I can't find an example and String.indexOf is case
sensitive and it is very easy to make Matcher.find() not be which is
what I want for this application.
As to the cognoscenti, I meant using the + operator to concatenate a
String but if I can work a Vector in there somewhere I will :-).
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-06-25 08:15 -0700 |
| Message-ID | <3rplq9hoa7vg9b7oq6iqh4e5iesatvg1f5@4ax.com> |
| In reply to | #28800 |
On Fri, 20 Jun 2014 15:46:13 -0700, Knute Johnson <eternal@knutejohnson.com> wrote, quoted or indirectly quoted someone who said : >Well I've been round and round on this, I thought originally that I >could find all the examples of the search string with one call to >Matcher.matches() but I can't figure out how to do that I have examples of use of all the regex methods in little SSCCEs. See http://mindprod.com/jgloss/regex.html -- Roedy Green Canadian Mind Products http://mindprod.com Why program by hand in 5 days what you can spend 2 to 5 years of your life automating. ~ Terence Parr 1964-08-17
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-06-25 18:10 -0700 |
| Message-ID | <lofrui$n84$1@dont-email.me> |
| In reply to | #28848 |
On 6/25/2014 08:15, Roedy Green wrote:
> On Fri, 20 Jun 2014 15:46:13 -0700, Knute Johnson
> <eternal@knutejohnson.com> wrote, quoted or indirectly quoted someone
> who said :
>
>> Well I've been round and round on this, I thought originally that I
>> could find all the examples of the search string with one call to
>> Matcher.matches() but I can't figure out how to do that
>
> I have examples of use of all the regex methods in little SSCCEs.
>
> See http://mindprod.com/jgloss/regex.html
>
Thanks I'll take a look.
I think what I thought I could do is not possible. Apparently you can't
get multiple capture groups without multiple sets of parenthesis. I
thought I could do something like;
Pattern.compile("(the)*") and capture all of the 'the's in the string in
their own group. But as it turns out each group must have its own set
of parenthesis.
So instead of that approach I decided to use Matcher.find() to search
through the string and keep a list of the matches.
I'll post some code once I've got it cleaned up and debugged.
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-06-30 09:52 -0700 |
| Message-ID | <los4ji$tqb$1@dont-email.me> |
| In reply to | #28856 |
On 6/25/2014 18:10, Knute Johnson wrote:
>
> So instead of that approach I decided to use Matcher.find() to search
> through the string and keep a list of the matches.
>
> I'll post some code once I've got it cleaned up and debugged.
>
The point behind my regex questions were that I was trying to duplicate
the text search feature found on FireFox. On FireFox if you type CTRL-F
you get a text box and some controls on the bottom of the FireFox window
that you can type into and search the text on the displayed web page.
For my purposes I don't need the up and down arrows and the Match Case
features. I just wanted to find the text and be able to use the ENTER
and SHIFT-ENTER keys to move about and the ESC key to close the search
window.
The interesting thing that I found by accident on a web search is that a
JTextComponent that is not in focus will not display selected text. I
still need to come up with a simple reversible way to swap out the Caret
on the target JTextComponent when my SearchField is displayed.
Code is posted below and if you want to play with it, you can find the
source and a compiled jar at: http://knutejohnson.com/searchfield.
Thank you everybody that responded to my query.
package com.knutejohnson.components;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import com.knutejohnson.classes.*;
public class TestSearchField extends JFrame {
private final JTextArea textArea = new JTextArea(10,40);
private final SearchField searchField = new SearchField(10,textArea);
private final JScrollPane scrollPane =
new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
private boolean flag;
public TestSearchField() {
try {
URL url = new URL("http://knutejohnson.com/gba");
BufferedReader br = new BufferedReader(new InputStreamReader(
url.openStream()));
String str;
while ((str = br.readLine()) != null) {
textArea.append(str);
textArea.append("\n");
}
textArea.setCaretPosition(0);
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
TestSearchField.this.dispose();
}
});
add(scrollPane,BorderLayout.CENTER);
textArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
if (ke.isControlDown() && ke.getKeyCode() ==
KeyEvent.VK_F) {
if (!flag) {
flag = true;
TestSearchField.this.add(
searchField,BorderLayout.SOUTH);
TestSearchField.this.pack();
}
searchField.requestFocus();
}
}
});
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
searchField.addCloseListener(new CloseListener() {
public void close(CloseEvent ce) {
remove(searchField);
pack();
flag = false;
}
});
pack();
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new TestSearchField();
}
});
}
}
package com.knutejohnson.components;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import com.knutejohnson.classes.*;
/**
* Entering text into the SearchField causes a scan of the JTextComponent
* parameter. Matching text will be highlighted and the background color
* of the SearchField will be used to indicate successful/unsuccesful
searches
*
* @author Knute Johnson
*/
public class SearchField extends JTextField implements DocumentListener,
KeyListener {
/** background color to indicate a successful search */
private static final Color LIGHT_GREEN = new Color(220,255,220);
/** background color to indicate an unsuccessful search */
private static final Color LIGHT_RED = new Color(255,220,220);
/** the JTextComponent to be searched */
private final JTextComponent comp;
/** a list of begin and end locations of matching text */
private final java.util.List<Point> finds = new ArrayList<>();
/** the index of the currently highlighted find */
private int index;
/**
* Constructs a SearchField with the specified length and
JTextComponent
* to search.
*
* @param columns the number of columns to use to calculate the
* preferred width
* @param comp the {@code JTextComponent} to search
*/
public SearchField(int columns, JTextComponent comp) {
super(columns);
this.comp = comp;
getDocument().addDocumentListener(this);
addKeyListener(this);
// give the JTextComponent to be searched a new Caret that has a
// visible selection even when out of focus
comp.setCaret(new DefaultCaret() {
@Override public void setSelectionVisible(boolean visible) {
super.setSelectionVisible(true);
}
});
}
/**
* Method of the KeyListener interface that is used to detect ENTER,
* SHIFT+ENTER and the ESC key presses
*
* @param ke the KeyEvent passed by in by the event producer
*/
@Override public void keyPressed(KeyEvent ke) {
// if ENTER has been pressed
if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
// if there are finds
if (finds.size() > 0) {
if (ke.isShiftDown()) {
if (--index < 0)
index = finds.size() - 1;
} else
if (++index >= finds.size())
index = 0;
// highlight the matching text
comp.setCaretPosition(finds.get(index).x);
comp.moveCaretPosition(finds.get(index).y);
}
}
// if ESC has been pressed
if (ke.getKeyCode() == KeyEvent.VK_ESCAPE)
// send a CloseEvent to all registered CloseListeners
fireCloseEvent();
}
/**
* Unused method of the KeyListener interface
*/
@Override public void keyReleased(KeyEvent ke) {
}
/**
* Unused method of the KeyListener interface
*/
@Override public void keyTyped(KeyEvent ke) {
}
/**
* Calls the search() method when an attribute changed
*/
@Override public void changedUpdate(DocumentEvent de) {
search();
}
/**
* Calls the search() method when there was an insert into the document
*/
@Override public void insertUpdate(DocumentEvent de) {
search();
}
/**
* Calls the search() method when part of the document was removed
*/
@Override public void removeUpdate(DocumentEvent de) {
search();
}
/**
* Method to perform the actual search of the {@code TextComponent}
*/
private void search() {
try {
Document doc = getDocument();
// if the SearchField document has no text
// clear the finds,
// set the search index to 0,
// color the SearchField white,
// remove any highlighted text,
// and return
if (doc.getLength() == 0) {
finds.clear();
index = 0;
setBackground(Color.WHITE);
comp.setCaretPosition(comp.getCaretPosition());
return;
}
// get the search string
String str = doc.getText(0,doc.getLength());
// create the Pattern and Matcher
Pattern p = Pattern.compile(Pattern.quote(str),
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(comp.getText());
// clear the finds
finds.clear();
// look for and save all found locations of the search string
while (m.find())
finds.add(new Point(m.start(),m.end()));
// if matches were found
if (finds.size() > 0) {
// set the SearchField background to green and
// mark the first find
setBackground(LIGHT_GREEN);
comp.setCaretPosition(finds.get(0).x);
comp.moveCaretPosition(finds.get(0).y);
index = 0;
// otherwise
} else {
// set the SearchField background to red
setBackground(LIGHT_RED);
comp.setCaretPosition(comp.getCaretPosition());
}
} catch (BadLocationException ble) {
ble.printStackTrace();
}
}
/** list of CloseListeners */
private final java.util.List<CloseListener> closeListeners =
new ArrayList<>();
/**
* Adds a CloseListener to the list
*
* @param cl CloseListener to add
*/
public void addCloseListener(CloseListener cl) {
closeListeners.add(cl);
}
/**
* Removes a CloseListener from the list
*
* @param cl CloseListener to remove
*/
public void removeCloseListener(CloseListener cl) {
closeListeners.remove(cl);
}
/**
* Sends a CloseEvent to all CloseListeners in the list
*/
protected void fireCloseEvent() {
CloseEvent ce = new CloseEvent(this);
for (CloseListener listener : closeListeners)
listener.close(ce);
}
}
package com.knutejohnson.classes;
import java.awt.*;
import java.util.*;
/**
* The {@code CloseEvent} class is an event type to deliver a close message
*
* @author Knute Johnson
*
* @see CloseListener
*/
public class CloseEvent extends EventObject {
/**
* Constructs a new {@code CloseEvent}
*
* @param source The Object generating the event
*/
public CloseEvent(Object source) {
super(source);
}
}
package com.knutejohnson.classes;
/**
* The {@code CloseListener} interface is implemented by classes wishing to
* listen for a {@code CloseEvent}
*
* @author Knute Johnson
*/
public interface CloseListener {
/**
* Override this method to receive {@code CloseEvent}s
*
* @param ce The CloseEvent
*/
public void close(CloseEvent ce);
}
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-06-21 09:49 -0700 |
| Message-ID | <5sdbq9pcumn5g82un8uudib0484kqbvbiu@4ax.com> |
| In reply to | #28788 |
On Thu, 19 Jun 2014 22:03:10 -0700, Knute Johnson <eternal@knutejohnson.com> wrote, quoted or indirectly quoted someone who said : > >I hate that, look for it for an hour, write something here and then come >across the answer 2 minutes later. see http://mindprod.com/jgloss/regex.html I has the basics on one page. -- Roedy Green Canadian Mind Products http://mindprod.com Why program by hand in 5 days what you can spend 2 to 5 years of your life automating. ~ Terence Parr 1964-08-17
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-08-01 02:45 -0700 |
| Message-ID | <e9omt9tjc03mnfnrig68us1067oqitf45t@4ax.com> |
| In reply to | #28787 |
On Thu, 19 Jun 2014 21:39:36 -0700, Knute Johnson <eternal@knutejohnson.com> wrote, quoted or indirectly quoted someone who said : >How do you search for a String that contains a character such as ( that >is special to a regex? I solved unworking Regex after unworking Regex with RegexBuddy that lets you debug regexes in isolation and a bit at a time. Experimenting is much faster than a compile test cycle, and you can see more what is going on. See http://mindprod.com/jgloss/regexbuddy.html -- Roedy Green Canadian Mind Products http://mindprod.com The art of strongly-typed language design is largely arranging that errors are automatically detected as soon as possible in the compose, compile, run cycle.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web