9

I've found several online tools that allow me to see the effect of a regular expression I have created on sample text, but I am looking for a tool that would make expression suggestions based on a portion of text selected.

For Example:

Let's say I have a string like this,

obssoCookie=set-usermember1-404343994;Version=1;Path=/;Domain=10.118.195.239;Discard

and I want to extract everything between obssoCookie= and the first ; I'd like a tool that would allow me to select that part of the text and respond with a suggested regular expression.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
urbanaut
  • 741
  • 2
  • 11
  • 26

5 Answers5

12

Check out txt2re.

Using your sample text, with the relevant portions selected, and C# selected for the code to be generated, this is the result.

It takes some getting used to, but the basic steps are:

  1. Select the link for individual characters or whole words desired based on the colored boxes generated on the page.
  2. Select the desired language.
  3. The pattern is built up for you in the generated sample code.
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
  • 2
    Can you get this site to just spit out the plain regex text rather than in code? – urbanaut Jun 02 '11 at 20:48
  • @urbanaut not that I know of. It shouldn't be too hard to take the concatenated string and use it for your purposes, but I agree that having the site do that would've been convenient. – Ahmad Mageed Jun 02 '11 at 20:54
  • 31
    Unfortunately http://www.txt2re.com/ is now dead – Daniil Shevelev Jan 13 '20 at 21:24
  • 2
    Anyone found any good alternatives to txt2re ? – rollsch Jan 04 '21 at 01:29
  • @rollsch See the answers [here](https://stackoverflow.com/questions/15512918/grammatical-inference-of-regular-expressions-for-given-finite-list-of-representa): there are several other tools that can generate regular expressions from example strings. – Anderson Green Feb 11 '22 at 15:30
4

I found this recently, uses genetic algorithm to find best matching pattern given a few examples that you generate yourself:

http://regex.inginf.units.it/

mmagician
  • 1,970
  • 2
  • 15
  • 26
1

This website (txt2re.com) probably does what you want. It will generate regular expressions and code to use them for a number of languages. Very cool.

Rick Goldstein
  • 299
  • 1
  • 6
0

Try this: /obssoCookie=([^;]*);.*/

Example using Javascript:

var mts = "obssoCookie=set-usermember1-404343994;Version=1;Path=/;Domain=10.118.195.239;Discard";
var regEx = /obssoCookie=([^;]*);.*/;
alert(mts.replace(regEx, "$1"));
Chandu
  • 81,493
  • 19
  • 133
  • 134
  • Turns out this got everything between specified beginning and end points: (obssoCookie=)(.*?)\; Thank you though! – urbanaut Jun 02 '11 at 20:41
-1

I have used this tool for over a year and will NEVER go back to txt2re:

Regexr:

Browser-based

Desktop-based (Win/Osx/Nux)

QA Automator
  • 307
  • 2
  • 12
  • 1
    I'm sorry I misunderstood the question that you wanted a tool that would suggest the regex for you. Regexr highlights the matches as you build the expression, but does not make suggestions. – QA Automator Jun 02 '11 at 20:37
  • 1
    I had found this site, love it, just lacks the ability to suggest expressions. – urbanaut Jun 02 '11 at 20:49