Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:13:48+00:00 2026-05-26T14:13:48+00:00

I’m trying to create a regex pattern to match the lines in the following

  • 0

I’m trying to create a regex pattern to match the lines in the following format:

field[bii] = float4:.4f_degree  // Galactic Latitude
field[class] = int2  (index) // Browse Object Classification
field[dec] = float8:.4f_degree (key) // Declination
field[name] = char20  (index) // Object Designation
field[dircos1] = float8   // 1st Directional Cosine

I came up with this pattern, which seemed to work, then suddenly seemed NOT to work:

field\[(.*)\] = (float|int|char)([0-9]|[1-9][0-9]).*(:(\.([0-9])))

Here is the code I’m trying to use (edit: provided full method instead of excerpt):

private static Map<String, String> createColumnMap(String filename) {

    // create a linked hashmap mapping field names to their column types. Use LHM because I'm picky and
    // would prefer to preserve the order
    Map<String, String> columnMap = new LinkedHashMap<String, String>();

    // define the regex patterns
    Pattern columnNamePattern = Pattern.compile(columnNameRegexPattern);

    try {
        Scanner scanner = new Scanner(new FileInputStream(filename));
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();

            if (line.indexOf("field[") != -1) {
                // get the field name
                Matcher fieldNameMatcher = columnNamePattern.matcher(line);
                String fieldName = null;
                if (fieldNameMatcher.find()) {
                    fieldName = fieldNameMatcher.group(1);
                }

                String columnName = null;
                String columnType = null;
                String columnPrecision = null;
                String columnScale = null;
                //Pattern columnTypePattern = Pattern.compile(".*(float|int|char)([0-9]|[1-9][0-9])");
                Pattern columnTypePattern = Pattern.compile("field\\[(.*)\\] = (float|int|char).*([0-9]|[1-9][0-9]).*(:(\\.([0-9])))");
                Matcher columnTypeMatcher = columnTypePattern.matcher(line);

                System.out.println(columnTypeMatcher.lookingAt());

                if (columnTypeMatcher.lookingAt()) {
                    System.out.println(fieldName + ": " + columnTypeMatcher.groupCount());
                    int count = columnTypeMatcher.groupCount();
                    if (count > 1) {
                        columnName = columnTypeMatcher.group(1);
                        columnType = columnTypeMatcher.group(2);
                    }
                    if (count > 2) {
                        columnScale = columnTypeMatcher.group(3);
                    }
                    if (count >= 6) {
                        columnPrecision = columnTypeMatcher.group(6);
                    }
                }

                int precision = Integer.parseInt(columnPrecision);
                int scale = Integer.parseInt(columnScale);

                if (columnType.equals("int")) {
                    if (precision <= 4) {
                        columnMap.put(fieldName, "INTEGER");
                    } else {
                        columnMap.put(fieldName, "BIGINT");
                    }
                } else if (columnType.equals("float")) {
                    if (columnPrecision==null) {
                        columnMap.put(fieldName,"DECIMAL(8,4)");
                    } else {
                        columnMap.put(fieldName,"DECIMAL(" + columnPrecision + "," + columnScale + ")");
                    }
                } else {
                    columnMap.put(fieldName,"VARCHAR("+columnPrecision+")");
                }
            }

            if (line.indexOf("<DATA>") != -1) {
                scanner.close();
                break;
            }
        }

        scanner.close();
    } catch (FileNotFoundException e) {

    }

    return columnMap;
}

When I get the groupCount from the Matcher object, it says there are 6 groups. However, they aren’t matching the text, so I could definitely use some help… can anyone assist?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:13:49+00:00Added an answer on May 26, 2026 at 2:13 pm

    It’s not entirely clear to me what you’re after but I came up with the following pattern and it accepts all of your input examples:

    field\\[(.*)\\] = (float|int|char)([1-9][0-9]?)?(:\\.([0-9]))?
    

    using this code:

        String columnName = null;
        String columnType = null;
        String columnPrecision = null;
        String columnScale = null;
        // Pattern columnTypePattern =
        // Pattern.compile(".*(float|int|char)([0-9]|[1-9][0-9])");
        // field\[(.*)\] = (float|int|char)([0-9]|[1-9][0-9]).*(:(\.([0-9])))
        Pattern columnTypePattern = Pattern
                .compile("field\\[(.*)\\] = (float|int|char)([1-9][0-9]?)?(:\\.([0-9]))?");
        Matcher columnTypeMatcher = columnTypePattern.matcher(line);
    
        boolean match = columnTypeMatcher.lookingAt();
        System.out.println("Match: " + match);
    
        if (match) {
            int count = columnTypeMatcher.groupCount();
            if (count > 1) {
                columnName = columnTypeMatcher.group(1);
                columnType = columnTypeMatcher.group(2);
            }
            if (count > 2) {
                columnScale = columnTypeMatcher.group(3);
            }
            if (count > 4) {
                columnPrecision = columnTypeMatcher.group(5);
            }
            System.out.println("Name=" + columnName + "; Type=" + columnType + "; Scale=" + columnScale + "; Precision=" + columnPrecision);
        }
    

    I think the problem with your regex was it needed to make the scale and precision optional.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.